Passing Data Between Activities

Moving From One Activity to Another Activity

Start Another Activity

You can start another activity within the same application by calling startActivity(), passing it an Intent that describes the activity “class name” you want to start.
An intent contains the action and optionally additional data. The data can be passed to other activity using intent putExtra() method.

Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);

The complete sendMessage() method.

/** Called when the user clicks the Send button */
public void sendMessage(View view) {
    Intent intent = new Intent(this,SecondActivity.class);
    EditText editText = (EditText) findViewById(R.id.message);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}

Receive Data

You can get the Intent that started your activity by calling getIntent() and retrieve the data contained within the intent.

  1. In the res/layout directory, edit the content_second.xml file.
  2. Add an TextView element to the Layout.
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    

    Tip: android:textSize="20sp" for Change Text Size
           android:textColor="@color/blue" for Change Text Color
           android:textStyle="bold" for Change Text Style

  3. In the java/akraj.snow.test directory, edit the SecondActivity.java file.
  4. Get the intent and assign it to a local variable.
    Intent intent = getIntent();
  5. Extract the message delivered by MainActivity with the getStringExtra() method.
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    

    Tip: In Android Studio, press Alt + Enter (option + return on Mac) to import missing classes.

Passing Multiple Values

MainActivity:-

Intent i = new Intent(MainActivity.this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putString("Name", "Akshay Raj");
bundle.putString("Email", "[email protected]");
i.putExtras(bundle);
startActivity(i);

SecondActivity:-

Bundle bundle = getIntent().getExtras();
String name = bundle.getString("Name");
String email = bundle.getString("Email");
TextView textName = (TextView) findViewById(R.id.name);
TextView textEmail = (TextView) findViewById(R.id.email);
textName.setText(name);
textEmail.setText(email);

 

Complete Code

content_main.xml (res/layout)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="akraj.snow.test.MainActivity"
    tools:showIn="@layout/activity_main">
    <EditText android:id="@+id/edit_message"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message" />
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send" />
</LinearLayout>

MainActivity.java (java/akraj.snow.test)

package akraj.snow.test;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
    public final static String EXTRA_MESSAGE = "akraj.snow.test.MESSAGE";
    EditText editText;
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        editText = (EditText) findViewById(R.id.edit_message);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                String message = editText.getText().toString();
                intent.putExtra(EXTRA_MESSAGE, message);
                startActivity(intent);
            }
        });
    }
}

content_second.xml (res/layout)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="akraj.snow.test.SecondActivity"
    tools:showIn="@layout/activity_second">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:id="@+id/textView" />
</RelativeLayout>

SecondActivity.java (java/akraj.snow.test)

package akraj.snow.test;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
        TextView textView = (TextView) findViewById(R.id.textView);
        textView.setText(message);
    }
}

Manifest File

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="akraj.snow.test">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SecondActivity"
            android:label="@string/title_activity_second"
            android:parentActivityName=".MainActivity"
            android:theme="@style/AppTheme">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="akraj.snow.test.MainActivity" />
        </activity>
    </application>
</manifest>

You can now run the app. When it opens, type a message in the text field, and click Send. The second activity replaces the first one on the screen, showing the message you entered in the first activity.
 

About the author

Akshay Raj

View all posts