Android Tutorials

How to Send Data from One Activity to Another Activity – Android

Hello developers, today we shall see one of the important tasks we encounter while developing apps. That is how to send data from one activity to another activity. What I mean by data is either strings or integers or boolean values or anything that can be quantified.

send data from one activity to another

First of all, let’s see how to switch from one activity to another, later on we will send some data along with it.

To move from Activity 1 to Activity 2, we shall use Intents. For example,

 Intent i = new Intent(Activity1.this,Activity2.class);
startActivity(i); 

In the above code, Activity1 is the name of the activity from which you are moving to Activity2. Note that you have to use .this for the referring activity and .class for the referred activity if you are extending Activity class. You can also use getApplicationContext() or getActivity() in place of Activity1.this depending on the scenario in which you are in.

Example:

 

 Intent i = new Intent(getApplicationContext(),Activity2.class);
startActivity(i); 

or

 Intent i = new Intent(getActivity(),Activity2.class);
startActivity(i); 

And if you don’t want to get back to previous activity after clicking back button, then you have to declare finish() after you start the intent .

 Intent i = new Intent(getApplicationContext(),Activity2.class);
startActivity(i);
finish();

Now that you know how to move from one activity to another activity, let us add some more code so that we can send data from one activity to another.

What we are going to use is putExtra function which will take the data with it. Let us see in the example below in which we are sending a String value.

Intent myintent = new Intent(MainActivity.this, MainActivity2.class).putExtra("StringName", stringValue);
startActivity(myintent);

In the above code, StringName is the name you are giving for the string you are sending (which is later required when getting the value in 2nd activity) and stringValue is the value you are sending.

Please note that the stringValue you are sending can be any type. i.e., it can be a string, an integer, a boolean, a float value, etc. But you need to be careful when you are receiving it in the 2nd Activity.

Now that we have sent the data, let us see the retrieving part in the below code. We use getStringExtra() function.

String rec_value;
rec_value=getIntent().getStringExtra("StringName");

In the above code, we assumed that we sent a string value from the first activity so we are assigning the string value to a variable String anmed rec_value. Hope you are getting it. If we send an integer data, all we have to is declare the rec_value as int. Let me show you.

int rec_value;
rec_value=getIntent().getStringExtra("StringName");

Do remember that the name “StringName” should be same which you sent from Activity1.

You now know how to send data from one activity to another using Intents.

Now let us see a working example below.

Create a sample xml as activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Tutorial 3"
android:textColor="#FF0000"
android:textSize="25dp"
android:textStyle="bold|italic" />

<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="This is the First Activity"
android:textSize="25dp" />

<EditText
android:id="@+id/stringText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter anything" />

<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="NEXT Activity"

/>
</LinearLayout>

Create another xml activity_main2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Tutorial 3"
android:textColor="#FF0000"
android:textSize="25dp"
android:textStyle="bold|italic" />

<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:text="This is the Second Activity"
android:layout_gravity="center"/>
<EditText
android:id="@+id/stringText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter anything" />
<Button
android:id="@+id/prev"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:text="Prev Activity"
android:layout_height="wrap_content"

/>
</LinearLayout>

Now let us create two activities MainActivity1.java and MainActivity2.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
 //Declare the TextViews/EditTexts/Buttons

Button next;
 EditText eT;
 String stringValue;

@Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main); //Enter the layout name you want
 eT = (EditText) findViewById(R.id.stringText);
 next = (Button) findViewById(R.id.next);

next.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View view) {
 stringValue = eT.getText().toString();
 //Go from Present Activity to Another Activity along with the value 'stringValue' with name 'StringName'
 Intent myintent = new Intent(MainActivity.this, MainActivity2.class).putExtra("StringName", stringValue);
 startActivity(myintent);

}
 });

}

}
import android.app.Activity;
 import android.content.Intent;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.Button;
 import android.widget.EditText;
public class MainActivity2 extends Activity {
//Declare the TextViews/EditTexts/Buttons

Button prev;
EditText eT;
String rec_value;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2); //Enter the layout name you want

//Initialise with corresponding ID
prev = (Button) findViewById(R.id.prev);
eT=(EditText) findViewById(R.id.stringText2);
rec_value=getIntent().getStringExtra("StringName");

eT.setText("You Received "+rec_value );
prev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Go from Present Activity to Another Activity
Intent i = new Intent(MainActivity2.this, MainActivity.class);
startActivity(i);
}
});

}

}

Declare MainActivity1.java as your launcher activity and MainActivity2.java as an activity in AndroidManifest.xml and run the project.

That’s all folks. Today you learnt how to send data from one activity to another activity using Intents. Hope this tutorial helped you. Please stay tuned to RandomBlog.Guru for more updates like these. If you want to see any other tutorials, please drop a comment below, I’ll try to make a detailed article on it. Thanks for reading. Please like our facebook page for regular updates.

About the author

Darshan Gowda

Follow me on Facebook, Google Plus

Leave a Comment

error: Content is protected !!