How to Pass Data Between Activities in Android

From WikiHTP

This example illustrates sending a String with the value Some data! from an activity called OriginActivity to another called DestinationActivity. This is the most straightforward way of sending data between two activities.

OriginActivity[edit]

public class OriginActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_origin);
        
        // Create a new Intent for the target Activity.
        final Intent intent = new Intent(this, DestinationActivity.class);

        // Attach data in the form of key/value pairs to the Intent by using putExtra().
        intent.putExtra(DestinationActivity.EXTRA_DATA, "Some data!");

        // Start the target Activity with the Intent
        startActivity(intent);
    }
}

DestinationActivity[edit]

public class DestinationActivity extends AppCompatActivity {

    public static final String EXTRA_DATA = "EXTRA_DATA";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_destination);

        // getIntent() returns the Intent which was used to start this Activity
        final Intent intent = getIntent();

        // Retrieve the data from the Intent by using the same key that
        // was used to attach it to the Intent in the OriginActivity.
        final String data = intent.getStringExtra(EXTRA_DATA); 
    }
}

It is also possible to pass other primitive data types as well as arrays, Bundle and Parcelable data. Passing Serializable is also possible, but should be avoided as it is more than three times slower than Parcelable.

Serializable is a standard Java interface. You simply mark a class Serializable by implementing the interface, and Java will automatically serialize it in certain situations.

Parcelable is an Android-specific interface which can be implemented on custom data types (i.e. your own objects), it allows your object to be flattened and reconstruct itself without the destination needing to do anything.

Once you have a parcelable object you can send it similarly to a primitive type. On an intent:

intent.putExtra(DestinationActivity.EXTRA_DATA, myParcelableObject);

Or in a bundle / as an argument for a fragment

bundle.putParcelable(DestinationActivity.EXTRA_DATA, myParcelableObject);

and then also read it from the intent in the destination as before:

final MyParcelableType data = intent.getParcelableExtra(EXTRA_DATA);

Or when reading in a fragment from a bundle

final MyParcelableType data = bundle.getParcelable(EXTRA_DATA);

Once you have a Serializable object you can send it on an intent:

bundle.putSerializable(DestinationActivity.EXTRA_DATA, mySerializableObject);

and then also read it from the intent in the destination as below:

final SerializableType data = (SerializableType)bundle.getSerializable(EXTRA_DATA);

About This Tutorial

This page was last edited on 28 January 2019, at 06:36.