Overview
In my previous article Return Data Using Intent Object in Android Applications, I discussed how to return the data using the intent object between two activities. If you are new to app creation in Android, refer to the following articles:
Introduction
In this article, I will explain about passing the data between two or more activities, using the Intent object in Android applications.
Coding
I made some changes in the design of the existing file, activity_my_second.xml.
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical">
- <TextView android:text="@string/MySecond_Activity"
- android:layout_width="fill_parent"
- android:layout_marginTop="20dp"
- android:layout_height="wrap_content" />
- <Button android:layout_height="wrap_content"
- android:layout_width="fill_parent"
- android:text="Return to Main Activity"
- android:id="@+id/btnOk"
- android:onClick="onClick" />
- </LinearLayout>
In MySecondActivity.java class, add a method onClick() and get the data passed from the main activity, using getIntent() and getStringExtra().
- package com.example.administrator.intentexampleapp;
-
- import android.content.Intent;
- import android.net.Uri;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.Toast;
-
- public class MySecondActivity extends ActionBarActivity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_my_second);
-
- Toast.makeText(this,getIntent().getStringExtra("string1"),Toast.LENGTH_SHORT).show();
-
-
- Toast.makeText(this,Integer.toString(getIntent().getIntExtra("Marks1",80)),Toast.LENGTH_SHORT).show();
-
-
- Bundle bundle=getIntent().getExtras();
-
-
- Toast.makeText(this, bundle.getString("string2"),Toast.LENGTH_SHORT).show();
-
-
- Toast.makeText(this,Integer.toString(bundle.getInt("Marks2")), Toast.LENGTH_SHORT).show();
- }
-
- public void onClick(View view)
- {
-
- Intent data= new Intent();
-
- data.putExtra("Marks3",70);
-
- data.setData(Uri.parse("Data passed to the Main Activity"));
-
- setResult(RESULT_OK,data);
-
- finish();
- }
- }
Add one button in the activity_main.xml file.
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical">
- <Button
- android:layout_width="fill_parent"
- android:layout_height="50dp"
- android:text="Show second activity"
- android:layout_marginTop="20dp"
- android:onClick="showSecondActivity" />
- </LinearLayout>
In the MainActivity.java class, implement the showSecondActivity() method.
- package com.example.administrator.intentexampleapp;
-
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.content.Intent;
- import android.widget.Toast;
-
- public class MainActivity extends ActionBarActivity {
- int request_Value=1;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- public void showSecondActivity(View view)
- {
-
- Intent intent= new Intent("android.intent.action.MySecondActivity");
-
-
- intent.putExtra("string1","This is first string");
- intent.putExtra("Marks1",80);
-
-
- Bundle bundle = new Bundle();
- bundle.putString("string2", "This is second string");
- bundle.putInt("Marks2", 60);
-
-
- intent.putExtras(bundle);
-
- startActivityForResult(intent, request_Value);
- }
-
- public void onActivityResult(int requestCode, int resultCode, Intent data){
- if(requestCode == request_Value){
-
- if(resultCode == RESULT_OK){
-
-
- Toast.makeText(this,Integer.toString(data.getIntExtra("Marks3",0)),Toast.LENGTH_SHORT).show();
-
-
- Toast.makeText(this,data.getData().toString(),Toast.LENGTH_SHORT).show();
- }
- }
- }
- }
Execute the application. It will show the following output:
![]()
Click on Show Second Activity button. Now, the data is passed to the second activity.
![]()
Click on
RETURN TO MAIN ACTIVITY button. Data from the second activity is passed to the main activity.
![]()
Explanation
In this app, we use the putExtra() method of an Intent object to add a name/value pair.
-
- intent.putExtra("string1","This is first string");
- intent.putExtra("Marks1",80);
Above statements add two name/value pairs to the Intent object. First one is of type
string and the second one is of type
integer.
Apart from this, create a Bundle object and attach it using the putExtras method(). Bundle object is just like a dictionary object. It contains a set of name/ value pairs. So, the following statements create a Bundle object, and then add two name/value pairs to it.
-
- Bundle bundle = new Bundle();
- bundle.putString("string2", "This is second string");
- bundle.putInt("Marks2", 60);
-
- intent.putExtras(bundle);
Call the second activity.
- startActivityForResult(intent, request_Value);
On the second activity, we first obtain the Intent object, using the getIntent() method. Then, call its getStringExtras() method
to get the string value set, using the putExtra() method.
-
- Toast.makeText(this,getIntent().getStringExtra("string1"),Toast.LENGTH_SHORT).show();
Now, we have to call method to extract the name/value pair, based on the type of data set. Use the getIntExtra() method for the integer value.
-
- Toast.makeText(this,Integer.toString(getIntent().getIntExtra("Marks1",80)),Toast.LENGTH_SHORT).show();
Retrieve the Bindle object by using getExtras() method.
-
- Bundle bundle=getIntent().getExtras();
Get the individual name/value pairs by using getString() method.
-
- Toast.makeText(this, bundle.getString("string2"),Toast.LENGTH_SHORT).show();
Get the integer value by using getInt() method.
-
- Toast.makeText(this,Integer.toString(bundle.getInt("Marks2")), Toast.LENGTH_SHORT).show();
From the second activity, pass the data to the Main activity by using setData() method, as I have already discussed in my previous article.
-
- Intent data= new Intent();
-
- data.putExtra("Marks3",70);
-
- data.setData(Uri.parse("Data passed to the Main Activity"));
-
- setResult(RESULT_OK,data);
-
- finish();
To retrieve the data set, using the setData() method, use the getData() method,
-
- Toast.makeText(this,Integer.toString(data.getIntExtra("Marks3",0)),Toast.LENGTH_SHORT).show();
-
-
- Toast.makeText(this,data.getData().toString(),Toast.LENGTH_SHORT).show();
Conclusion
In this article, we saw how to pass data from one activity to the second activity and vice versa. In the next article,
I will explain about Fragment.