The canonical way to run multiple nested actions and get the result onActivityResult

I have the following requirement:

Action A ---> Activity B ---> Open Gallery Application

Traditionally, I run nested actions using 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(); 

(The question is whether there is a difference between using the task stack constructor or calling startActivities ()).

The problem with this approach is that when galleryIntent is closed, it does not call onActivityResult , but calls the OnCreate ActivityB method, which means that I lose information coming from the gallery application, which is transmitted through the "data" of the intent parameters to my activity onActivityResult call B.

An alternative solution would be to manually initiate calls, so call the first call to Activity B, then with the flag / parameter / argument, run galleryIntent, and then execute a regular thread using onActivityResult .

Is there a better approach to solving this requirement?

+6
source share
1 answer

I have the feeling that TaskStackBuilder does not adapt very well to your needs. I would come to him easier.

* I assume that the interaction begins with activity A, and then you need to open the gallery, but operation B requires processing B.

I would open activity B and start the gallery intent. Once the gallery delivers the result to B, you can do any processing there. After additional processing, and if you need to, you can always deliver a different result from Activity B to A.

Please note that you need activity B to be already created and listen to the results before the gallery opens.

+3
source

Source: https://habr.com/ru/post/970912/


All Articles