In this read, we will see, how to share data using intent across different activities in Android app development. This post will help you to understand how the data is shared between activities inside the Let’s see how the data can be shared across the activities using Intent.
What is Intent in Android
The intent is a simple message object that is used for communicating between the Application components, like activity, content providers and broadcasting receivers and services. By using intent, data can be shared between the activities.
Also Read: Android SearchView in ActionBar— Androidx and Kotlin
How to call an Intent
In Android application development, the intent can be called using the startActivity()
method. Using this method the intent is passed as a parameter while calling this we can share the data to the different activities. It supports all types of primitive data types and also it helps to share a data class object too.
Type of data allowed to share
The intent allows the application to share any data type from an activity to another activity by using intent.putExtra(Key, value)
method.
By this way we can share the data to the different components and activities.
To get the data in activity the intent extra should be getting based on the data type, if the value is a string then intent.getStringExtra(key)
. Likewise, we can receive the data. If the shared data is parcelable
then use getParcelableExtra(key)
followed by the data type.
Methods used in data sharing
The extras shared in intent can be verified by hasExtra(key)
. This will return a boolean value. If the extra is attached to the intent the data returned upon using getExtra()
, if not null will be returned.
note: whenever getting the data from intent, it is a best practice to check for the availability of the data for the requesting key.
For more information on this please visit the developer’s documentation page for an updated one. That information is available on Content/Intent.
How to send and receive data
For example please look at the below code for how to send and receive the data.
Let’s consider we’re sharing a string extra through the intent. Here is how can we receive the data in NextActivity
class.
Other ways to send data
Similarly, the data between different activities can be shared using a static variable in the receiver class/activity. In this way, we don’t need the intent to share the extras/bundle with the receiver activity.
We can assign the data to the static variable before we start the particular activity/class. Also accessing the data in the receiver class is too easy in this way.
Limitations on sharing data
However, the intent is allowed to share the data to the activities and components, there are few limitations to it. For example, if we share a large amount of data through it it may result in an application crash.
As the memory for data sharing in the mobile application is limited sharing large data may arise an issue. So it’s better to use another method of data sharing. Like assigning a static variable. API call and other memory handlers.
Comments 1