Wednesday, September 29, 2010

Passing data to activity

Launching an activity is not all. After launching another subactivity/activity you might want to send some data/parameter to that activity. This is done in a new way known as Bundle. To launch an activity we create an intent. Suppose our intent name is "intent". We can put data with the intent like this

intent.putExtra("keyName", "somevalue");

We can add multiple entries here. This is a key,value pair. So to receive this data from the receiving activity we have to write this code

Bundle extras = getIntent().getExtras();
if(extras !=null)
{
String value = extras.getString("keyName");
}

This is how we can send data to another activity, here simple string is sent, complex structures like array can be also sent through bundle. Didn't use those as i am a bad bad programmmer and very bad at reading documentations ;).



Your Ad Here