Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Reddit
Contact us
Hide Buttons

Android Activities – Activity Send Data To Another Activity.

In our earlier post “Create and Start Activity” you have learnt

  • What is Activity
  • How to create an Activity?
  • How one activity starts another activity?

This Article we will extend it to – Activity can send some data to another activity Either using intent.putExtraOR using Bundle object and then attach it to putExtras().

Using putExtra() method :

first way is to use the putExtra() method of an Intent object to add a kay/value pair.

putExtra(key,value) : This function takes key, value pair. The activity who is receiving the data, use this key to get the value.

Code at the Activity who is sending the data.

Intent i = new Intent(this, SecondActivity.class);

i.putExtra(MESSAGE, s);

i.putExtra("Int1",25);

startActivity(i);

 

Code at the Activity who is receiving the data.

Intent intent = getIntent();

String msg = intent.getStringExtra(MainActivity.MESSAGE);

 

using Bundle object and putExtras():

Another way is to use Bundle object. Create a bundle object and put the data in it using key, value pair. This way you can group all the related data into a bundle.

Code at the Activity who is sending the data.

Intent intent = getIntent();

Bundle bundle = new Bundle();

String str2 = "coming through bundle";

bundle.putString("string2", str2);

bundle.putInt("int_id_2", 200);

i.putExtras(bundle);

startActivity(i);

Code at the Activity who is receiving the data.

Intent intent = getIntent();

Bundle b = getIntent().getExtras();

String str2 = b.getString("string2", "");

int int2 = b.getInt("int_id_1");

Complete Example :

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.simpleactivity"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name = "com.example.simpleactivity.SecondActivity" />"
    </application>

</manifest>

First Activity:

First Activity

First Activity

MainActivity.java

package com.example.simpleactivity;

import android.app.Activity;
import android.content.Intent;
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 MainActivity extends Activity {

	EditText msg ;
	public static final String MESSAGE = "com.example.SIMPLE_MESSAGE";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        msg =(EditText) findViewById(R.id.msg);

    }
    public void startSecond(View v){
    	String s = msg.getText().toString();

    	Intent i = new Intent(this, SecondActivity.class);
    	i.putExtra(MESSAGE, s);
    	i.putExtra("int_id_1", 100);

    	Bundle bundle = new Bundle();
    	String str2 = "coming through bundle";
    	bundle.putString("string2", str2);
    	bundle.putInt("int_id_2", 200);
    	i.putExtras(bundle);
    	String toast = "Sending data : " + s + " and Integer 100 \n Sending through Bundle, String : "+ str2+" and Integer : 200";
    	Toast.makeText(this,toast , Toast.LENGTH_SHORT).show();
    	startActivity(i);
    }
}

 

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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="First Activity"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/msg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="text" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20sp"
        android:layout_gravity="center"
        android:text="Start Second Activity"
        android:onClick="startSecond" />

</LinearLayout>

SecondActivity:

Second Activity

Second Activity

SecondActivity.java

package com.example.simpleactivity;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class SecondActivity extends Activity {
	 @SuppressLint("NewApi") @Override
	    protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.second);
	        Intent intent = getIntent();
			String msg = intent.getStringExtra(MainActivity.MESSAGE);
			int Int1 = intent.getIntExtra("int_id_1",0);
	        Bundle b = getIntent().getExtras();
	        String str2 = b.getString("string2", "");
	        int int2 = b.getInt("int_id_1");
	        String toast = "Received Data : String - " + msg + "Integer - " + String.valueOf(Int1) + " \n " + "Received Data through Bundle : String - " + str2 + "Integer - " + String.valueOf(int2) ;
	        Toast.makeText(this, toast  , Toast.LENGTH_LONG).show();
	    }
}

second.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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Second Activity"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

Output :

Send Data Output

Send Data Output

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.

You may also like...

1 Response

  1. December 12, 2014

    […] Android Activities — Activity Send Data To Another Activity. […]

Leave a Reply

Your email address will not be published. Required fields are marked *