Press enter to see results or esc to cancel.

Android Intents and Extras – putExtra and getExtra Example

Share this

In Android App Development, When we talked about the components that represent the building blocks of an android app, the role of the Android Intents was mentioned. Going deeper, we can say that they represent a form of messaging managed by the operating system with which a component can request the execution of an action by another component.

The most common uses of Android Intents falls into these three cases:

  1. Start an Activity;
  2. Start a Service;
  3. Send a broadcast message that can be received by each application.

Android Intents Extras – Android App Development

Another very useful aspect of the Intents is that they, in delivering this message, have at their disposal a sort of “trunk”, in which they keep data that can be read by the recipient. These values ​​shared through Intent are generally called Extras and can be of various types, both belonging to more common classes than to others provided they can be serialized. The management of Extras in Intents works in a similar way to a map data structure: with put methods, a value labeled with a key is entered and with the corresponding get methods the value is taken, requesting it using the recognition key.

Lesson 10 – Android Intents and Extras – Android App Development

Intent Extra Example – Login Form – Android App Development

Let us take as an example the most common case, usually used early by the neophyte Android programmer, the activation of one Activity by another. Let’s look at what appears in the figure:

We have two activities :

  1. MainActivity contains a simple login form. After entering the username and password the validity of the data is checked and in the positive case the opening of another activity is invoked;
  2. SecretActivity is the area accessible only through login and contains confidential data.

At the entrance of the second Activity, the application wants to welcome the user but to do so it needs to know what it’s called.

Everything you need is already included in the Intent mechanism.

At the code level, in the MainActivity, once we get the success in the login we will find:

Intent i=new Intent(this, SecretActivity.class);
i.putExtra("username", account_username);

The three operations represent:

  • Intent declaration as a normal Java object. In this case, we will have a so-called explicit intent as it clearly appears the name of the class that will be invoked;
  • A String is inserted between the Extras, the variable account_username, which will be transported with the Intent to the destination, which is the SecretActivity class. The purpose is to insert in this string the name of the user who logged in. Note that the extra is labeled with a key, in this case, “username”. This is because more Extras can be carried for each Intent and it must be possible to distinguish them to use them;
  • Finally the method startActivity demonstrates what action we want to activate with this Intent, in this case, it is the start of an Activity.

In the method onCreate of the second Activity, the one with the reserved content, we will find the following lines:

Intent i = getIntent();
String username = i.getStringExtra("username");

We immediately note that the activated Activity is available, through getIntent(), the Intent that caused its activation. You can use it to retrieve the string passed, containing the user name.

Lesson 10 – Android Intents and Extras – Android App Development

At the life cycle level, what happens?

The transition from one Activity to another involves the life cycles of both. The first one, the one set aside, will have to pass at least for onPause (cessation of interaction with the user) and onStop (activity no longer visible) while the second will run along the creation chain onCreate– onStart– onResume.

But in what order will all this happen? The system priority is maintaining the fluidity of the user-experience. For this reason, the following operations will be:

  • the first Activity goes through onPause and is stopped in the Paused state;
  • the second Activity goes in Running and is activated completely. In this way the user will be able to use it as soon as possible without being delayed;
  • at this point, while the user is already using the second Activity, the system can invoke onStop on the first.

How useful was this Lesson?

Click on a star to rate it!

Average rating / 5. Vote count:

Be the first to rate this post.!

As you found this post useful...

Follow us on social media!

We are sorry that this lesson was not useful for you!

Let us improve this lesson!

Tell us how we can improve this lesson?

Share this