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 ear­lier post “Cre­ate and Start Activ­ity” you have learnt

  • What is Activity
  • How to cre­ate an Activity?
  • How one activ­ity starts another activity?

This Arti­cle we will extend it to — Activ­ity can send some data to another activ­ity Either using intent.putExtraOR using Bun­dle object and then attach it to putEx­tras().

Using putEx­tra() method :

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

putExtra(key,value) : This func­tion takes key, value pair. The activ­ity who is receiv­ing the data, use this key to get the value.

Code at the Activ­ity who is send­ing the data.

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

i.putExtra(MESSAGE, s);

i.putExtra("Int1",25);

startActivity(i);

 

Code at the Activ­ity who is receiv­ing the data.

Intent intent = getIntent();

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

 

using Bun­dle object and putExtras():

Another way is to use Bun­dle object. Cre­ate a bun­dle 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 Activ­ity who is send­ing 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 Activ­ity who is receiv­ing the data.

Intent intent = getIntent();

Bundle b = getIntent().getExtras();

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

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

Com­plete 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 Activ­ity:

First Activity

First Activ­ity

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>

Sec­on­dAc­tiv­ity:

Second Activity

Sec­ond 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>

Out­put :

Send Data Output

Send Data Output

You may also like...

1 Response

  1. December 12, 2014

    […] Android Activ­i­ties — Activ­ity Send Data To Another Activity. […]

Leave a Reply

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