Whenever you need data from an activity to be in another activity, you can pass data between then while starting the activities. Intents in android offer this convenient way to pass data between activities using Extras. Creating multiple activities to display contents of same properties is not an ideal solution. Instead, passing data from one activity to another and displaying related info is a better way. We can achieve other solutions using the same idea.
We’ll start of with the basic concept of starting an activity from a current activity. We use
Intent to achieve this. As the name suggests Intents are intentions to perform something. They are basically massage passed between components (Activities, Services, Broadcast receivers etc). To start an activity we’ll create an object of the intent and pass it to the
/* Creates an Intent object by giving the context and the class of next activity to be opened */ Intent intent = new Intent(this, NewActivity.class); //starting the activity startActivity(intent);
We can start adding data into the Intent object, we use the method defined in the Intent class
putExtras() to store certain data as a key value pair or Bundle data object. These key-value pairs are known as Extras in the sense we are talking about Intents.
We’ll see an example of storing a string in this Intent object using a key.
//creating and initializing an Intent object Intent intent = new Intent(this, NextActivity.class); //attach the key value pair using putExtra to this intent String user_name = "Jhon Doe"; intent.putExtra("USER_NAME", user_name); //starting the activity startActivity(intent);
It is a good practice to use the key as capital letters since the data received at the next activity will be eventually treated as immutable.
We can also use the same
putExtra() method to attach Integers, floating point values, bytes, characters and a few other data types as listed on the official android developers documentation.
Retrieving data from intent
Once you start the activity, You’ll be able to get the data attached in the next activity [or services, broadcast receivers.. etc] being started. to retrieve the data, we use the method
getExtra() on the NextActivity from the intent.
The following example will show you how to use getExtra() on the target activity is started.
//get the current intent Intent intent = getIntent(); //get the attached extras from the intent //we should use the same key as we used to attach the data. String user_name = intent.getStringExtra("USER_NAME"); //if you have used any other type of data, you should use the //particular getExtra method to extract the data from Intet Integer user_id = intent.getIntExtra("USER_ID"); float user_rating = intent.getFloatExtra("USER_RATING");
Using Bundle to help it make easier
You should have noticed, this is a not so cool and easy way to send quite a number of items. This is where we use Bundles. Bundle is a mapping from String keys to various parcelable values. We can store any number of key-value pairs in a Bundle object and simply pass this object through the intent.
//create a Bundle object Bundle extras = new Bundle(); //Adding key value pairs to this bundle //there are quite a lot data types you can store in a bundle extras.putString("USER_NAME","jhon Doe"); extras.putInt("USER_ID", 21); extras.putIntArray("USER_SELCTIONS", [1, 2, 3, 4, 5]); ... //create and initialize an intent Intent intent = new Intent(this, NextActivity.class); //attach the bundle to the Intent object intent.putExtras(extras); //finally start the activity startActivity(intent);
It’s quite the same on retrieving the bundle attached to the intent. We can use the
Intent.getExtras() in the target activity/class to get the attached bundle and extract the data stored in it.
//get the intent in the target activity Intent intent = getIntent(); //get the attached bundle from the intent Bundle extras = intent.getExtras(); //Extracting the stored data from the bundle String user_name = extras.getString("USER_NAME"); Integer user_id = extras.getInt("USER_ID"); ... ...
Passing Objects as serializable
We can also pass Java class objects through bundles, maybe an ArrayList, HashMap, or any type of object, we can serialize in one end get it at the other end.
To put an object to a bundle, we use
Bundle bundle = new Bundle; bundle.putSerializable("KEY", YOUR_OBJECT); intent.putExtras(bundle);
To retrieve this object, in the target activity, we simply get the serializable and assign it to an object of same type by typecasting the return value of
Intent intent = this.getIntent(); Bundle bundle = intent.getExtras(); //Type object = (Type) bundle.getSerializable("KEY"); DataClass myData = (DataClass) bundle.getSerializable("MY_DATA"); //we can send ArrayList, HashMaps or any serializable objects //Use the Type of the object instead of DataClass //in the ablove example
Hope you have understood how to use store and extract data into intents using Extras. For further information you can visit the official android developer website for learning more about Intents, putExtra, and Bundles. Comment down below if you ran into any error, we’re happy to help.