0

I am fairly new at android and I am unsure about intents.

If I have 3 activities, A B and C, and activity A needs to receive bundles from both B and C at different times (eg: moving from B to A, or from C to A), how would I go about doing that?

Hopefully this question isn't too vague...

Mr DC
  • 33
  • 5
  • u should work on framing your question properly before u ask here – Dinesh Venkata Mar 29 '12 at 17:14
  • ? his question is perfectly fine – dymmeh Mar 29 '12 at 17:39
  • just get your extras and test if they aren't null , and then you can work with them without knowing from which activity are sended , and if you need to know the activity that send the extras , you just add a boolean to know if your activity is launched from the Activity B or C – Houcine Mar 29 '12 at 17:41
  • I don't think the question is fine. I'm not sure what 'receiving bundles' is. A finishing activity doesn't send any bundles back to the previous activity. I presume the submitter wanted to know how to exchange data between activities. – Alexander Kulyakhtin Mar 29 '12 at 17:44

2 Answers2

0

To start activity B from activity A you can call

    final int REQ_STARTB = 101; // anything non-zero
    startActivityForResult(REQ_STARTB, new Intent(A.this, B.class)). 
    //Similar for C.

Then when you have some data from B and want to pass them back to A you can do from B:

Intent I = new Intent()
I.putExtra("MyStringData", stringYouWantToReturn);
//and similar for other types 
setResult(RESULT_OK);
finish();

So it goes back to A and in A you will have

public onActivityResult(int req, int res, Intent data) {
    if(req == REQ_STARTB) {
        if(res == RESULT_OK) {
            String dataFromB = data.getStringExtra("MyStringData");
        }
     }
}
Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
-1

You can call Activity A from either Activity B or C at any time. You just create a new Intent sending it your current context and the Activity you want to call. E.g.:

Intent i = new Intent(this, ActivityB.class);

Then start your intent by calling:

startActivity(i);

Bear in mind, you'll have to make sure all your Activities are defined in your manifest.

Karim Varela
  • 7,562
  • 10
  • 53
  • 78