Android - How to Pass Data From One Activity to Second Activity

Sovary May 8, 2022 969
3 minutes read

This article shows you how to send data from one activity to another activity with Java and Kotlin languages. In this example we have two activities, the first activity is the source activity (First_activity.java) or (First_activity.kt) and the second activity is the destination activity (Second_activity.java) or (Second_activity.kt).

We can send data using the putExtra() and putSerializable() method of one activity and get data for the second activity.

Pass Data From One Activity to Second Activity

Send Data Methods

  • putExtra() method is used for sending the data, data in key-value pair. Key is variable name and value can be primitive data type (Int, String, Float...)
  • putSerializable() method is used for sending the data as object serializable, data in key-value pair. Key is variable name and value can be object instance of class.

Get Data Methods

  • getStringExtra() method is for get the data(key) that is send by putExtra() method. This is depend on the data type, there are other methods such as getIntExtra(), getSerializableExtra(), and getFloatExtra()...
  • getSerializableExtra() method is for get data which known as object.

1. Send and get data as string or a primitive value

1. We will create the Intent object First_activity.java class with Second_activity.java class

Java

Intent intent = new Intent(getApplicationContext(), Second_activity.class);

Kotlin

val intent = Intent(applicationContext,Second_activity::class.java)

Method getApplicationContext() or applicationContext will fetch the current activity.

2. Add the value in the putExtra method to the key-value pair and start the activity. This is done as follows:

Java

intent.putExtra("data_key", "This is string data");
startActivity(intent);

Kotlin

intent.putExtra("data_key", "This is string data")
startActivity(intent)

"data_key" is the key to identify the data we will use this key in Second_activity to populate the string value "This is string data"

3. In Second_activity.java or Second_activity.kt we create the object of getIntent() or intent to get the value in String type variable by getStringExtra() method using with "data_key".

Java

String str = getIntent().getStringExtra("data_key");

Kotlin

val str:String = intent.getStringExtra("data_key")

2. Send and get data as an object value (custom class, built-in class)

1. We will create the Intent object First_activity.java class with Second_activity.java class

Java

Intent intent = new Intent(getApplicationContext(), Second_activity.class);

Kotlin

val intent = Intent(applicationContext, Second_activity::class.java)

2. Add the object in the putSerializable() method to the key-value pair from bundle data. This is done as follows:

Java

Userinfo userinfo = new Userinfo();
userinfo.setName(username);
Bundle bundle = new Bundle();
bundle.putSerializable("data_key", userinfo);

Kotlin

val userinfo = Userinfo()
userinfo.name= "example"
val bundle = Bundle()
bundle.putSerializable("data_key", userinfo)

where Userinfo is a customized class that implements from Serializable interface and we use Bundle object to put data as an object.

3. We put the bundle into putExtra() method to pass the data and start launch Second_activity.java

Java

intent.putExtra(bundle);
startActivity(intent);

Kotlin

intent.putExtra(bundle)
startActivity(intent)

4. In Second_activity.java or Second_activity.kt we create the object of getIntent() or intent to get the value object type variable by getSerializableExtra() method using with "data_key".

Java

Userinfo info = (Userinfo) getIntent().getSerializableExtra("data_key");

Kotlin

val info = intent.getSerializableExtra("data_key") as Userinfo

We actually know this serialize data is from a specific type of Userinfo class, so we can cast the object into Userinfo

Full Code In First_Activity.java

Intent intent = new Intent(getApplicationContext(), Second_activity.class);
intent.putExtra("data_key", "This is string data")
startActivity(intent)

Full Code In Second_Activity.java

String str = getIntent().getStringExtra("data_key");
Android  Kotlin  Java 
Author

As the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him    

Search