Passing data between Intents or classes in ANDROID?

Many of our application reqiures sending data from one intent to another.
This is done by putting data into one intent using putExtras() and getting it in the other intent using the getExtras() which matches the string value. However you can pass string, boolean, integer etc between intents.
For passing data between intents you need atleast two intents.
Here the two activities are named DataBetweenIntentsExample and Result
Make sure to add the two activities in your manifest file, otherwise your application will force close.
Here is an example showing how to pass data between intents in ANDROID.
First we create the layout.
Here I am crearting a simple layout with only one button and a textBox in it.
The main.xml file looks like this.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
	android:text="Site Name : "
	android:id="@+id/TV1"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content">
</TextView>

<EditText
	android:text=""
	android:id="@+id/site_name"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content">
</EditText>

<Button
	android:text=" Send Data "
	android:id="@+id/B1"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content">
</Button>

</LinearLayout>

Then you create a java file named Result.xml and place it inside the current package.
Then create a layout for the Result.xml file and name it result.xml and place it in the layout directory.

The result.xml file will look like this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
	android:text=""
	android:id="@+id/result_tv"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content">
</TextView>

</LinearLayout>

Then we go to the main java file DataBetweenIntentsExample.java and copy this code into it.

package pack.DataBetweenIntents;

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

public class DataBetweenIntentsExample extends Activity {

	public Button B1;
	public EditText T1;
	public Intent intent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        B1 = (Button)findViewById(R.id.B1);
        T1 = (EditText)findViewById(R.id.site_name);

        intent = new Intent();
		intent.setClass(this,Result.class);

        B1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				intent.putExtra("site_name",T1.getText().toString());
				startActivity(intent);

			}
		});
    }
}

Now copy the following code to Result.java file.

package pack.DataBetweenIntents;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Result extends Activity {

	public TextView TV;
	 @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.result);

	        TV = (TextView)findViewById(R.id.result_tv);
	        Bundle bundle = getIntent().getExtras();

	        if(bundle.getString("cancel") != null){
	        	TV.setText("Cancelled");
	        }else{
	        	String txt1 = bundle.getString("site_name");
	        	TV.setText("Data send from previous intent nSite Name = " + txt1 );
	        }
	    }
}

Androidmanifest.xml file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="pack.DataBetweenIntents"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".DataBetweenIntentsExample"
                  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=".Result"
         		  android:label="Results"
        />
</manifest>

One thought on “Passing data between Intents or classes in ANDROID?

  1. Pingback: How to use global variables in android? -Part 2 | Coderz Heaven

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>