Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Canonical way of starting multiple nested activities and getting a result onActivityResult

Tags:

android

I have the following requirement:

Activity A ---> Activity B ---> Open Gallery App

Traditionally, i launch nested activities using the TaskStackBuilder. So I would do something like this:

                TaskStackBuilder tsb = TaskStackBuilder.create(this);
                Intent activityIntentA = new Intent(this, ActivityA.class) // ...
                tsb.addNextIntent(activityIntentA);

                Intent activityIntentB = new Intent(this, ActivityB.class) // ...
                tsb.addNextIntent(activityIntentB);

                Intent galleryIntent = new Intent(Intent.ACTION_PICK);
                galleryIntent.setType("image/*");
                tsb.addNextIntent(galleryIntent);

                // this.startActivities(new Intent[] {activityIntentA, activityIntentB, galleryIntent});
                tsb.startActivities();

(A side question is if there's a difference between using a task stack builder or the startActivities() call).

The problem with this approach though, is that when the galleryIntent is closed, it doesn't call onActivityResult but rather calls the OnCreate method of ActivityB, which means i lose the information coming in from the gallery app, which is supplied through the intent param "data" on my onActivityResult call of activityB.

An alternative solution, would be to manually kick off the calls, so call first call Activity B, then with a flag/param/argument, launch the galleryIntent, and then follow the regular flow through with OnActivityResult.

Is there a better approach to solving this requirement?

like image 874
Kaushik Gopal Avatar asked Jun 16 '14 23:06

Kaushik Gopal


1 Answers

I have the feeling that TaskStackBuilder doesn't adapt very well to your needs. I'd approach it in an easier manner.

*I'm assuming the interaction starts on activity A, and then you need to open the gallery, but need activity B to process the result.

I'd open activity B and launch the intent for the gallery from there. As soon as gallery delivers a result to B you can do any processing there. After the extra processing and if you need to, you can always deliver another result from Activity B to A.

Note that you need activity B to be already created and listening for results before the gallery gets open.

like image 139
Jose L Ugia Avatar answered Nov 10 '22 00:11

Jose L Ugia