Tuesday, January 22, 2008

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 ;).

20 Comments:

  1. Thomas Hays said...
    Thanks for that post, it solved a big problem with my program! Do you by any chance know how to do something when a widget is swiped?
    Shimul said...
    Sorry, i am out of touch with android for a long time. It's good to know that something of me came into your use.
    Ben said...
    Thank you very much for the post - really helped me out :)
    JW said...
    thanks it helped me as well.
    Unknown said...
    Great stuff man! Well written and concise. Thanks!
    - Satyajeet Shaligram.
    GOLDSQUID said...
    ok i got how to pass the data to another but the question is how to connect the activity to avtivity and then pass the data
    please Email me on vishalgaikwad20@gmail.com
    GOLDSQUID said...
    I have got the passing the data to activity but how to call the another activity from one activity and pass the data
    Abir said...
    Thanks a lot!
    Great tutorial, it really helps. Thanks again...
    Anonymous said...
    Thanks!
    Anonymous said...
    thanks... helped me a lot
    mattia lipreri said...
    Thank you, really helpful!
    CardinalFIB said...
    This was very helpful. Now how do you pass parameters back from the receiving intent activity to the original activity?
    IMAPP said...
    you should make your data structure parcelable, and pass it through intent
    Jeevan said...
    This comment has been removed by the author.
    Jeevan said...
    Thanks, got rid of a blocking issue with this piece of code.
    Keerthy said...
    Yea that's great, i too saw in a list activity example on this site http://android-codes-examples.blogspot.com/2011/04/listactivity-with-remembering-last.html
    Atheist said...
    Great short example, easy to understand, no unnecessary fluff to confuse. I had a simple question and you provided at simple answer, thanks a lot :)
    Kyle said...
    Thanks -- just curious about good practices. Where should I grab the data in Activity B? OnCreate, OnStart, does it matter?

    Thanks for the help!
    Android developer said...
    Now a days android is one of popular application and most of the peoples are like this android. so your articles good one for lots of android peoples.

    Android developer
    Unknown said...
    Thanks for post, nice article. Easy to understand . :)

Post a Comment