How to Pass Data from One Activity to Another using Intent Android Studio (2021)

How to Pass Data from One Activity to Another using Intent Android Studio (2021)

 


Introduction 


In this post we will learn how to pass data from one activity to another activity in android.

So, we will use Intent to pass data. Let's start with the coding part

  • putExtra() is the method used to put our data from the source activity. This is quite similar to the hashmap technique where we have unique key and value pair.

Source Activity code : 

2
3

Intent intent = new Intent(this,MainActivity2.class);
intent.putExtra("sample_data","Hello there this is sample data");
startActivity(intent);


this -> activity context

  • getStringExtra() is the method used for receiving the data from source activity.

Target Activity code : 


2
3

String myData = getIntent().getStringExtra("sample_data");


In myData string you'll be having your data.

Make sure that the keyword used is same otherwise the data will not be received.

That's all Please comment your doubts below.

Post a Comment

1 Comments