Passing Data Using An Intent Object In Android Applications

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.

  1. <LinearLayout  
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical">  
  6.   <TextView android:text="@string/MySecond_Activity"  
  7.       android:layout_width="fill_parent"  
  8.       android:layout_marginTop="20dp"  
  9.       android:layout_height="wrap_content" />  
  10.   <Button android:layout_height="wrap_content"  
  11.       android:layout_width="fill_parent"  
  12.       android:text="Return to Main Activity"  
  13.       android:id="@+id/btnOk"  
  14.       android:onClick="onClick" />  
  15. </LinearLayout>   

In MySecondActivity.java class, add a method onClick() and get the data passed from the main activity, using getIntent() and getStringExtra().

  1. package com.example.administrator.intentexampleapp;  
  2.    
  3.  import android.content.Intent;  
  4.  import android.net.Uri;  
  5.  import android.support.v7.app.ActionBarActivity;  
  6.  import android.os.Bundle;  
  7.  import android.view.Menu;  
  8.  import android.view.MenuItem;  
  9.  import android.view.View;  
  10.  import android.widget.EditText;  
  11.  import android.widget.Toast;  
  12.    
  13.  public class MySecondActivity extends ActionBarActivity {  
  14.    
  15.      @Override  
  16.      protected void onCreate(Bundle savedInstanceState) {  
  17.          super.onCreate(savedInstanceState);  
  18.          setContentView(R.layout.activity_my_second);  
  19.          // Here we get the data passed in using getStringExtra() method.  
  20.          Toast.makeText(this,getIntent().getStringExtra("string1"),Toast.LENGTH_SHORT).show();  
  21.    
  22.          // Here we the data passed in using getIntExtra() method.  
  23.          Toast.makeText(this,Integer.toString(getIntent().getIntExtra("Marks1",80)),Toast.LENGTH_SHORT).show();  
  24.    
  25.          // Get the Bundle object  
  26.          Bundle bundle=getIntent().getExtras();  
  27.    
  28.          // Here we get the data using the getString() method.  
  29.          Toast.makeText(this, bundle.getString("string2"),Toast.LENGTH_SHORT).show();  
  30.    
  31.          // Here we get the data using the geInt() method.  
  32.          Toast.makeText(this,Integer.toString(bundle.getInt("Marks2")), Toast.LENGTH_SHORT).show();  
  33.      }  
  34.    
  35.      public void onClick(View view)  
  36.      {  
  37.          //create an instance of the Intent object to return data.  
  38.          Intent data= new Intent();  
  39.          // Here we use the putExtra() method to return some value  
  40.          data.putExtra("Marks3",70);  
  41.          // Here we use the setData() method to return some value  
  42.          data.setData(Uri.parse("Data passed to the Main Activity"));  
  43.          // Here we use result with OK  
  44.          setResult(RESULT_OK,data);  
  45.          //Destroy the current activity  
  46.          finish();  
  47.      }  
  48.  }  
Add one button in the activity_main.xml file.
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.      xmlns:tools="http://schemas.android.com/tools"  
  3.      android:layout_width="fill_parent"  
  4.      android:layout_height="fill_parent"  
  5.      android:orientation="vertical">  
  6.      <Button  
  7.          android:layout_width="fill_parent"  
  8.          android:layout_height="50dp"  
  9.          android:text="Show second activity"  
  10.          android:layout_marginTop="20dp"  
  11.          android:onClick="showSecondActivity" />  
  12.  </LinearLayout>  
In the MainActivity.java class, implement the showSecondActivity() method.
  1. package com.example.administrator.intentexampleapp;  
  2.    
  3.  import android.support.v7.app.ActionBarActivity;  
  4.  import android.os.Bundle;  
  5.  import android.view.View;  
  6.  import android.content.Intent;  
  7.  import android.widget.Toast;  
  8.    
  9.  public class MainActivity extends ActionBarActivity {  
  10.      int request_Value=1;  
  11.      @Override  
  12.      protected void onCreate(Bundle savedInstanceState) {  
  13.          super.onCreate(savedInstanceState);  
  14.          setContentView(R.layout.activity_main);  
  15.      }  
  16.      public void showSecondActivity(View view)  
  17.      {  
  18.          //Created an instance of second activity using Intent.  
  19.          Intent intent= new Intent("android.intent.action.MySecondActivity");  
  20.    
  21.          //Here we use putExtra() to add new name/value pairs.  
  22.          intent.putExtra("string1","This is first string");  
  23.          intent.putExtra("Marks1",80);  
  24.    
  25.          //Here we use a Bundle object to add new name and values pairs  
  26.          Bundle bundle = new Bundle();  
  27.          bundle.putString("string2""This is second string");  
  28.          bundle.putInt("Marks2"60);  
  29.    
  30.          // Attach the Bundle object to the Intent object  
  31.          intent.putExtras(bundle);  
  32.    
  33.          startActivityForResult(intent, request_Value);  
  34.      }  
  35.    
  36.      public void onActivityResult(int requestCode, int resultCode, Intent data){  
  37.          if(requestCode == request_Value){  
  38.              //if the result is OK  
  39.              if(resultCode == RESULT_OK){  
  40.    
  41.                  // Here we get the result using getIntExtra() method  
  42.                  Toast.makeText(this,Integer.toString(data.getIntExtra("Marks3",0)),Toast.LENGTH_SHORT).show();  
  43.    
  44.                  //Here we get the result using getData() method.  
  45.                  Toast.makeText(this,data.getData().toString(),Toast.LENGTH_SHORT).show();  
  46.              }  
  47.          }  
  48.      }  
  49.  }  
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.
  1. //Here we use putExtra() to add new name/value pairs.  
  2.  intent.putExtra("string1","This is first string");  
  3.  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(). A 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.
  1. //Here we use a Bundle object to add new name and values pairs  
  2.  Bundle bundle = new Bundle();  
  3.  bundle.putString("string2""This is second string");  
  4.  bundle.putInt("Marks2"60);  
  5. // Attach the Bundle object to the Intent object  
  6.  intent.putExtras(bundle);  
Call the second activity.
  1. 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.
  1. // Here we get the data passed in using getStringExtra() method.  
  2.  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.
  1. // Here we the data passed in using getIntExtra() method.  
  2. Toast.makeText(this,Integer.toString(getIntent().getIntExtra("Marks1",80)),Toast.LENGTH_SHORT).show();   
Retrieve the Bindle object by using getExtras() method.
  1. // Get the Bundle object  
  2.  Bundle bundle=getIntent().getExtras();  
Get the individual name/value pairs by using getString() method.
  1. // Here we get the data using the getString() method.  
  2.  Toast.makeText(this, bundle.getString("string2"),Toast.LENGTH_SHORT).show();  
Get the integer value by using getInt() method.
  1. // Here we get the data using the geInt() method.  
  2.  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.
  1. //create an instance of the Intent object to return data.  
  2.  Intent data= new Intent();  
  3.  // Here we use the putExtra() method to return some value  
  4.  data.putExtra("Marks3",70);  
  5.  // Here we use the setData() method to return some value  
  6.  data.setData(Uri.parse("Data passed to the Main Activity"));  
  7.  // Here we use result with OK  
  8.  setResult(RESULT_OK,data);  
  9.  //Destroy the current activity  
  10.  finish();  
To retrieve the data set, using the setData() method, use the getData() method,
  1. // Here we get the result using getIntExtra() method  
  2.  Toast.makeText(this,Integer.toString(data.getIntExtra("Marks3",0)),Toast.LENGTH_SHORT).show();  
  3.    
  4.  //Here we get the result using getData() method.  
  5.  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.


Similar Articles