Canonical way of starting multiple nested activities and getting a result onActivityResult - 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?

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.

Related

finish() activity twice in android?

Okay say your using a app, and you opened a new activity and then opened another, you can end the activity your on by using finish(); and your back one activity, but how can you go back two activities, all the way back to the first one? I know you could use:
Intent savedGameIntent = new Intent(v.getContext(), firstclass.class);
v.getContext().startActivity(savedGameIntent);
But is that the best way to do it?
Use the flag Intent.FLAG_ACTIVITY_CLEAR_TOP.
Intent intent = new Intent(this,A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
From the documentation:
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.
So effectively, if you have A -> B -> C, and you intent to A with that flag set, B and C will close.
I believe that will start a new activity, not back up to the original one. It sounds like you want to finish the last and middle activities. If you start the last activity with startActivityForResult, you can then override onActivityResult in the middle activity, and call finish() from there.

Android - start multiple activities

is it possible to start multiple activities at once? I mean, from main create 3 activities in some order and just the last will be visible? Up to now, I was able to create only one activity.
Thanks
You might need something like this in order to launch deep into the app after the user has clicked a notification in order to display some newly added content, for example.
Intent i = new Intent(this, A.class);
i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(i);
Intent j = new Intent(this, B.class);
j.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(j);
Intent k = new Intent(this, C.class);
startActivity(k);
In this way you can start activities A, B and C at the same time and suppress transitions to activities A and B. You get a single transition from your current activity to activity C. I strongly suggest that you log the Activity lifecycle method calls (onCreate etc.) to LogCat, for example. It helps a lot in understanding the order of events.
This can be a common thing to do in response to deep linking or other use cases where you, basically, need to synthetically rebuild the Task (and all the activities it should contain). Sometimes, just specifying parents in the manifest isn't enough.
Take a look at TaskStackBuilder. One common example:
TaskStackBuilder.create( context )
.addNextIntent( intentOnBottom )
// use this method if you want "intentOnTop" to have it's parent chain of activities added to the stack. Otherwise, more "addNextIntent" calls will do.
.addNextIntentWithParentStack( intentOnTop )
.startActivities();
Really old question but I thought I still answer it.
Use:
public void startActivities (Intent[] intents, Bundle options)
Try startActivity(new Intent(...); at the end of your onCreate-Method of the first Activity.
This will immediatly launch a new Activity and pause the first one.
With back-key you will get back to the last Activity

Android New Intent New Screen

I am still not completely sure of this opening a new screen with a new intent. I have two problems. 1 is getting it to work and the second is more theory.
Firstly I have two packages com.quiz.max and com.reason.max both have activities names accordingly eg Quiz and Reason respectively. Here is the on click code I am trying to execute at the moment in quiz to go to reason.
Intent intent = new Intent();
intent.setClassName("com.reason.max", "com.reason.max.Reason");
this.startActivityForResult(intent, requestCode);
Secondly I heard if I start this intent then everytime i click the button a new intent is created. Does this mean if the user goes to reason page and navigates back and clicks the button again they actually create a new intent instead of going back to the already active one. Thus dozens could be opened via this method. Therefore should I close each reason intent once navigated back or is this a redundant point?
Max
I think you want
Intent intent = new Intent(this, Reason.class);
startActivityForResult(intent, requestCode);
Secondly, you don't "start an intent". You use an intent to ask an Activity to start, in this case the Reason activity. And yes, the default behavior is to start a new instance of the activity each time it is requested.
You can alter this behavior with the launchMode.
Make sure you read and understand the Activity lifecycle. You don't need to worry about too many Activities in existence, Android will handle that for you, but you should properly save state and clean up connections in the appropriate lifecycle methods.

Launching activity through intents

I am implementing a simple app. I need to start an activity based on the state of the Activity. Lets take i am using a button to start the activity.
1. If the activity is not started, I need to start XYZ activity.
2. If the XYZ activity is on focus, then i need to close the activity on the button press.
3. If the XYZ activity is not in focus (like onPause) state then, I need to change the button state.
Can you please help me in the flags that i need to use for starting the intent.
Is it possible to get the state of activity before I start that activity?
Try this
Intent intent = new Intent(currentActivity.this, callingActivity.class);
startActivity(intent);
You can use intent like this to call an activity
The button press will need to be captured by each activity separately, so just code the different responses into each different activity.
First off create a MAIN.java activity that is going to house your other activities. Like others have said you're going to have to code the button captures yourself because that should be common sense if you're trying to deal with intents. When you get that together though, you can start a new activity through intent like so:
// allocate new intent, initialized to the activity you wish to launch
Intent i = new Intent(this, ActivityToBeLaunched.class);
// put information into intent
i.putExtras("KeyName", value); // where "KeyName" is simply a reference string
// and 'value' can be anything from boolean - string.
// launch activity and wait for response
startActivityForResult(i, REQUEST_CODE);
Then within your ActivityToBeLaunched.java class you'll have an oncreate that will pull information from the intent like such:
// get intent
Intent i = this.getIntent();
// get information from intent
booleanVariable = i.getExtras().getBoolean("KeyName");
When you're done with this activity simply use;
// create intent
Intent i = new Intent();
// put information into result to send back to parent
i.putExtras("KeyName", value);
// set the result to be returned
setResult(i, ResultCode);
// finish child, return to parent with results
finish();

How can I receive CameraActivities result in a DIFFERENT Activity (i.e. not in the launching one)?

I've got Activity A which fires up the Camera intent via:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
After the picture is taken I can easily grab the picture in:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
But I'd like to receive the result in Activity B in which the image can be edited.
Right now I'm receiving the result in Activity A and pass it over to Activity B which results in showing the GUI of Activity A for a short while:
Intent i = new Intent().setAction("DisplayJPEG");
i.setClass(this, EditImageActivity.class);
i.putExtra("IMAGE_URI", uri);
startActivityForResult(i, REQUEST_EDIT_IMAGE);
Of course, I will need the result from Activity B in Activity A after the image has been edited. But that should work with:
setResult(resultCode, data);
So there has to be a way to do what I need. Please point me into the right direction.
Have you tried launching ActivityB, and in ActivityB onCreate event launch the Camera Intent?
You technically can't do what you're asking. You'll need to find a way to continue passing it the way you are and hide the UI or do as Pentium says and do it the other way around.
Edit: Nevermind, I misread how this works. What actually happens is you can use Activity A to start Activity B for result, but then if Activity B needs to start Activity C to continue processing whatever Activity A wanted, you can use FLAG_ACTIVITY_FORWARD_RESULT to make Activity C return its result to Activity A not B.
I haven't looked into this more than a quick glance, but I noticed an Intent flag called FLAG_ACTIVITY_FORWARD_RESULT which according to the documentation:
If set and this intent is being used to launch a new activity from an existing one, then the reply target of the existing activity will be transfered to the new activity. This way the new activity can call setResult(int) and have that result sent back to the reply target of the original activity.
Like I said, I haven't experimented with this, but that seems to suggest that you could launch your camera intent from Activity A but have it forward its result to Activity B.

Categories

Resources