Passing data between activities

Download android eBook

Example

This example illustrates sending a String with value as "Some data!" from OriginActivity to DestinationActivity.

NOTE: This is the most straightforward way of sending data between two activities. See the example on using the starter pattern for a more robust implementation.

OriginActivity

public class OriginActivity extends AppCompatActivity {

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

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

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

DestinationActivity

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 object which was used to start this Activity
        final Intent intent = getIntent();

        // Retrieve the data from the intent object by using the same key that
        // was previously used to add data to the intent object in 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 as Serializable by implementing the Serializable interface and Java will automatically serialize it during required situations.

Parcelable is an Android specific interface which can be implemented on custom data types (i.e. your own objects / POJO objects ), it allows your object to be flattened and reconstruct itself without the destination needing to do anything. There is a documentation example of making an object parcelable.

Once you have a parcelable object you can send it like a primitive type, with an intent object:

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 at the destination using getParcelableExtra:

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 put it in an intent object:

bundle.putSerializable(DestinationActivity.EXTRA_DATA, mySerializableObject);

and then also read it from the intent object at the destination as shown below:

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

Stats

Contributors: 31
2017-07-05
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook