I have activity A that starts activity B which starts activity C:
A -> B -> C
When a user clicks on a button in activity C, I want to bring A to the top of the stack and take B & C completely out of the stack. Is there a way to do this?
You can use the FLAG_ACTIVITY_CLEAR_TOP flag on the intent to restart activity A.
Related
I have activity A that starts activity B which starts activity C:
A -> B -> C
When a user clicks on a button in activity C, I want to bring A to the top of the stack and take B & C completely out of the stack. Is there a way to do this?
You can use the FLAG_ACTIVITY_CLEAR_TOP flag on the intent to restart activity A.
I'm stuck with an Activity backstack question. Let's say I have 5 activities in my backstack: like Activity A, Activity B, Activity C, Activity D and Activity E. At some point I want the user to go to another Activity G, when pressed on the back button on Activity E. Activity G needs to be put after Activity B, so I want Activity C and Activity D removed from the backstack (otherwise the user would go to Activity D).
Current situation A --> B --> C --> D --> E
Preferred situation A --> B --> G
Now I understand I can use FLAG_ACTIVITY_CLEAR_TOP when Activity G would have been in the backstack. But the Activity isn't. Also I don't want to use FLAG_ACTIVITY_NEW_TASK because then Activities A and B would also be gone.
Another approach would be to put
android:noHistory="true"
within the manifest for Activities C and D, but this would make the user go back to Activity B every time the user pressed the back button from within Activity C or D.
Who can point me in the right direction?
You can try below
C ----startActivityForResult----------> D ---startActivityForResult--> E
handle onActivityResult with result accordingly to finish Activities, make sure its chained action calls
When you start activity from C->D you put finish();
Intent intent=new Intent(C.this,D.class);
startActivity(intent);
finish();
same way for D->G this way it is possible.
Is there a way to start a task with an activity B in the foreground but with an activity A on the backstack? So when I start B via intent and press the back or home button A is active.
At the moment I can start a new task via intent flag FLAG_ACTIVITY_NEW_TASK and start B but of course my backstack is empty so when I hit the back button it will return to the home screen.
Only if the app was running before (activity A already exists). Starting activity B will result in the correct backstack state A,B where B in ontop of A.
when u backpress from that condition then set one flag boolean and whenever u backpress then check that falg if true then in back button hust pass Activity intent for A :)
I've requirement for application to set a Buttonin every activity to go back to HomeActivitybut I should Not Reload the content for it, so I need to re-use the instance I already have of HomeActivity, how I could do that?
You should use: FLAG_ACTIVITY_REORDER_TO_FRONT
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_REORDER_TO_FRONT
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. For example, consider a task
consisting of four activities: A, B, C, D. If D calls startActivity()
with an Intent that resolves to the component of activity B, then B
will be brought to the front of the history stack, with this resulting
order: A, C, D, B. This flag will be ignored if
FLAG_ACTIVITY_CLEAR_TOP is also specified.
I used FLAG_ACTIVITY_CLEAR_TOP for home button in my activities. If you have your HomeActivity already in application stack, this flag causes close of all activities above your HomeActivity. It depends if you need to reorder HomeActivity to front (Back button will return you back to activity where you clicked home) or you want to close all activities that are above HomeActivity (like clicking back until I'm in HomeActivity, in my case Back button closes application from my home activity).
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP
Support App starts activity A, then A starts activity B and finishes itself. After that activity B starts activity C.
Now the stack contains B and C, with C at the top.
Then I click a button in activity C, and want it to clear B and C and start activity A, i.e. I want activity A to be the only activity in the stack. How can I make it?
Edit: I made a test to use FLAG_ACTIVITY_CLEAR_TOP. But it didn't work in my case, because activity A is not running when button in activity C is clicked.
Set the FLAG_ACTIVITY_CLEAR_TOP flag on your intent to start activity A.
Edit: Is there a reason you can't leave A going? Then you could do as suggested.
Otherwise, another (more complicated) option:
In B start C forResult. When A is started from C, you could finish C with a result indicating to B to also exit.