Here's what I want to do:
If I use startActivity I start a new Activity and I can't use StartActivityForResult from Main Menu -> Total. I would like to go back on the Main Menu activity already present in my stack and remove Activities A, B ,C and Total.
Have you tried using FLAG_ACTIVITY_CLEAR_TOP flag in your intent (used to start "Main Menu" from "Total")?
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP
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.
To remove activities A,B,C, and Total from your stack, just call finish() on these 4 activites.
See also : Tasks & Back stacks and Activity task Design
Related
I was going through Android Task and Back Stack documentation and at one point they mention this:
if your application allows users to start a particular activity from more than one activity, a new instance of that activity is created and pushed onto the stack (rather than bringing any previous instance of the activity to the top). As such, one activity in your application might be instantiated multiple times (even from different tasks), as shown in figure 3. As such, if the user navigates backward using the Back button, each instance of the activity is revealed in the order they were opened (each with their own UI state)
Let's take an example:
I have Activity A Starting Activity B which Starts Activity C which starts D.
Stack is A->B->C->D now it is possible to Start C from D so when we start C from D stack will be
A->B->C->D->C
Now instead of this standard behavior I want Activity to have only 1 instance or only 1 entry in the Back Stack. "SingleTop" will not work since Activity C was not on top when we started it from D.
I might be missing something but is there any way to achieve this making sure an activity has only 1 backstack entry?
Thanks
Pranay
Use Intent.FLAG_ACTIVITY_CLEAR_TOP, e.g.:
Intent intent = new Intent(context, <your_activity_here>);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
So, all the activities in stack after activity C will be finished automatically. If you use the specified flag
A->B->C->D
will become
A->B->C
You can also use android:launchMode="singleInstance" in your activity tag in manifest
I'm just learning Android programming. The way I understand it is that the services work like a stack, Is there a way for a activity to return to the first activity that started the app, instead of just the previous one.
Example, say I have 4 activities, a,b,c,d. Is there a way for activity d to have a button that would bring up activity a?? Instead of activity d going to c, and c going back to b???
You can start again activity a, but using a flag in your intent:
FLAG_ACTIVITY_REORDER_TO_FRONT
From android docs:
If set in an Intent passed to Context.startActivity(), this flag will cause the launched activity to be brought to the front of its task's history stack if it is already running.
Using this, you'll reuse the instance of activity a already running, instead of starting a new one. Note that doing this, the instances of activities b, c and d will remain in the back stack (now after activity a).
Otherwise, if you want to finish this activities (and remove them from the back stack) you can start activity a (from d) with this other flag:
FLAG_ACTIVITY_CLEAR_TOP
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.
EDIT: this is a good resource to read, if you haven't did it yet:
Tasks and Back Stack.
Yes, you can do that. You simply code an intent for activity a to the button.
Use the command
finish():
You will be reverted to the main page
or else write an intent pointing to a
You can send information to different Activities via Intents.
Intent myIntent = new Intent(this, AvitivityName.class);
startActivity(myIntent);
Don't forget to add your Activities in AndroidManifest.xml.
Here's some tutorial on Android Intents:
how to use Android intents.
how to switch to another activity with a button click.
Example: I have an android app with 3 activities that has the following behaviors:
A (Home) -> B -> C
Activity A launches Activity B
Activity B launches Activity C
When user is on Activity B and they hit the Back button, it takes them Activity A
When user is on Activity C and they hit the Back button, it takes them Activity B
What I would like is when user is on Activity C, if they hit the "My Root Activity" button, it will take them to Activity A without adding a new instance of Activity A to the back stack.
So I don't want to have:
1) A
2) A-B
3) A-B-C
4) A-B-C-A
What I would like is:
1) A
2) A-B
3) A-B-C
4) A
How can I do this?
Check out the intent stack machinery!
http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html
EDIT:
You can add "singleTaks" or "singleInstance" to your activity in the manifest file and implement the onNewIntent() method.
"singleTaks"
The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one.
"singleInstance"
Same as "singleTask", except that the system doesn't launch any other activities into the task holding the instance. The activity is always the single and only member of its task.
Check out this link
I have each child activity extend a base activity. In the base activity, I defined an onActivityResult() which will impose a finish() if the activity at the top of the stack set a specific resultCode. So if they hit the "My Root Activity" button on Activity C, it will recursively roll up to Activity A. The Back button maintains it's functionality.
This is a rare scenario as I would have used "singleTask" but launching Activity A involves reloading it's dependencies which I don't want to couple of Activity C.
What is the difference between Intent.FLAG_ACTIVITY_CLEAR_TOP and finish() in Android?
The differerence between these two are as follows:
1.finish() you can use to end the activity in which you are right now present and also it will end one activity at one time.
2.In case of FLAG_ACTIVITY_CLEAR_TOP,It will end all the activities those are on top of the current activities inside the stack.There may be more than one activity.
suppose you are starting activities one after another in the order
A-->B-->C-->D,ie activity B started from activity A,activity C started from activity B and so on.
Now calling startactivity(A) from activity D with intent flag FLAG_ACTIVITY_CLEAR_TOP finishes all activities in between (here B and C) and starts A.
calling Finish() from your activity closes current activity
finish() android uses to end the activity by calling it in program.
(Note, you can also use onDestroy()).
FLAG_ACTIVITY_CLEAR_TOP clears all the activities that are top of the current activities inside the Activity stack.
I have a main activity A. There are two scenarios
1) A launches B.
B has launchmode singleTask and is launched with FLAG_ACTIVITY_NEW_TASK.
now I have a menu option in B which performs a delete operation and starts the activity A.
2) A launches B, which launces C it also contains the menu option to perform delet opereation.
I want A to be started with clearing the stack in both the scenarios but the activities belonging to another task still present there
I am stuck is there a way to clear the stack.
try using following code on delete opereation on both B and C activity
Intent intent=new Intent(B.this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
If i talk about your second scenario then FLAG_ACTIVITY_TOP_CLEARwill clear the stack..
And your stack now will be only A instead of A-B-C-A.
and in second case AFAIU your problem you have only two activity A and B so if you want to restart A then after restarting A manually finish B.
Hope you got some trick.
Another thing if you have activity with launchmode=SingleTask then you can use flag_activity_brought_to_front.
That will act like
A-B-c to A
Why not finishing B and C before calling A again?
finish();
My method can not meet your goal A to be started with clearing the stack,But when user choose delet opereation and start B from A again the task that include A and B will be reset.
Use the flag FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET to mark the task will be clear when needed when you first start activity B from A, then if user choose delet opereation menu item from B or C, you set a flag, so next time from A(A should be single_task launch mode in manifest), you use the flag FLAG_ACTIVITY_RESET_TASK_IF_NEEDED to start B. This flag will clear B and all top of the B.
Hope this will help you :)