How to transfer data between two activities in android

The following post demonstrates how to use explicit Intents and how to transfer data between two activities.

The first activity will call the second one via an explicit intent. Once the user select the “Back” button on his phone the first activity will receive some hard-coded data from the sub-activity.

You can download this code: example

Create a new Android application.
Create two layouts. The first activity_one.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:tools="http://schemas.android.com/tools"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:background="@drawable/background"
                    android:gravity="center_horizontal"
                    android:orientation="vertical" >

 <TextView android:id="@+id/TextView01"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:minHeight="60dip"
          android:text="@string/text_tv_activity_one"
          android:textSize="20sp" />

 <Button android:id="@+id/Button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:onClick="onClick"
        android:text="@string/title_button_activity_one" />

</LinearLayout>

Create the second layout, your name activity_two.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="wrap_content"
              android:layout_height="match_parent"
              android:background="@drawable/background"
              android:gravity="center_horizontal"
              android:orientation="vertical" >

<LinearLayout android:layout_width="wrap_content"
              android:layout_height="wrap_content" >

<TextView android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/input_one"
          android:textSize="32dip" />

<EditText android:id="@+id/input1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/defaultt" />

</LinearLayout>

<LinearLayout android:layout_width="wrap_content"
              android:layout_height="wrap_content" >

<TextView android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/input_two"
          android:textSize="32dip" />

<EditText android:id="@+id/input2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/defaultt" />

</LinearLayout>

<Button android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="@string/finish"
        android:width="80dp" />

</LinearLayout>

Create the following coding for your two activities.


package com.thedeveloperworldisyours.datatransfer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class ActivityOne extends Activity {

	private static final int REQUEST_CODE = 10;

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

    public void onClick(View view) {
        Intent i = new Intent(this, ActivityTwo.class);
        i.putExtra("Value1", "This value one for ActivityTwo ");
        i.putExtra("Value2", "This value two ActivityTwo");
        // Set the request code to any code you like, you can identify the
        // callback via this code
        startActivityForResult(i, REQUEST_CODE);
      }

      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
          if (data.hasExtra("returnKey1")) {
            Toast.makeText(this, data.getExtras().getString("returnKey1"),
                Toast.LENGTH_SHORT).show();
          }
        }
      }

}

and the second activity.


package com.thedeveloperworldisyours.datatransfer;

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

public class ActivityTwo extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_two);
		Bundle extras = getIntent().getExtras();
		if (extras == null) {
			return;
		}
		String value1 = extras.getString("Value1");
		String value2 = extras.getString("Value2");
		if (value1 != null && value2 != null) {
			EditText text1 = (EditText) findViewById(R.id.input1);
			EditText text2 = (EditText) findViewById(R.id.input2);
			text1.setText(value1);
			text2.setText(value2);
		}
	}

	public void onClick(View view) {
		finish();
	}

	@Override
	public void finish() {
		Intent data = new Intent();
		// Return some hard-coded values
		data.putExtra("returnKey1", "Swinging on a star. ");
		data.putExtra("returnKey2", "You could be better then you are. ");
		setResult(RESULT_OK, data);
		super.finish();
	}
}

You can download this code: example

3 thoughts on “How to transfer data between two activities in android

  1. Hi there, You have done a great job. I’ll definitely digg it and personally suggest to my friends. I’m sure they’ll be benefited from this website.

  2. Howdy! Would you mind if I share your blog with my twitter group?
    There’s a lot of folks that I think would really
    appreciate your content. Please let me know.
    Thank you

Leave a Reply

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