Saturday, November 19, 2011

Passing data between activities or context objects

You can pass data between activities, broadcastReceivers, etc, by using Intent and Bundle Objects as follows:
Intent intent = new Intent(FROM_ACTIVITY, TO_ACTIVITY);
Bundle bundle = new Bundle();
bundle.putString("data", "Hello World!");
intent.putExtras(bundle);
Similarly, retreiving an object:
Bundle bundle = intent.getExtras();
String data = b.getInt("data", "");
The second argument is the default value. Notice that the bundle object is an argument of the onCreate method for every activity.

No comments:

Post a Comment