Android: Passing data to an Activity

In my recent Android project there was a requirement to pass data to other Activity.
You can start the activity using following code:

Intent i = new Intent(context, OtherActivity.class);
context.startActivity(i);

To pass around the data to other activity we can use putExtra() method of Intent class:

Intent i = new Intent(context, OtherActivity.class);
i.putExtra("key1", "value1");
i.putExtra("key2", "value2");
context.startActivity(i);

In the OtherActivity.class get the data using getIntent() method:

String value1 = getIntent().getExtras().getString("key1");
String value2 = getIntent().getExtras().getString("key2");
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s