Removing an Activity from History Stack [duplicate] - android

This question already has answers here:
Remove Activities from Stack History in Android
(5 answers)
Closed 9 years ago.
Let's Suppose that I have the Sequence of the following Activities :
A -> B -> C -> D -> E
when I be in E coming from D I want to do some Actions then Go back to D without Keeping the E in the Stack.. So if I have back to D and press Back Button I will return to C not E
how to do that ???

If you add this in your android manifest for activity E it will not appear in the History stack.
android:noHistory="true"

Instead of creating a new intent to go back you should call this.finish(); on the activities that you no longer want, this will be "called" when the user pressed the back button as well, also, if you are creating a new activity and you wish for the current activity to be skipped when pressing back you can still call this.finish(); and then you can call this.startActivity(new Intent(this, ActivityName.class));

I'm not sure if I completely understand this
if I have back to D and press Back Button I will return to C not E
but if I do all you have to do is call finish() when you are done in E then you will return to D, then to C when you press back in D. If this is not what you are talking about then please clarify your question. But, when you finish() and Activity then it is cleared off of the stack so you would never return to E until you start it again. You never would from D anyway by pressing the back button
Google I/O Navigation
In its most basic form, Activities are put on the stack on top of each other in the way they come in. If you leave one, by calling finish() or pressing back, then it is removed and you are taken to the one placed before it (where you came from). There is much more to that but that is the very basic of what happens.

I think this is how you did your activity stack
A -> B -> C -> D -> E -> D (launched by intent from E).
Now you want to go from the second D to C. To do that, in E, call finish() after you start D. This should remove E from the stack.

If I understood your problem correctly;
You goes to Activity-E in a sequence of A, B, C, D, E.
Now you will come at the Activity-D when you will press the back button.
And here is the problem: When you press again back button, it goes again to Activity-E but you want to go on Activity-C here.
So you can use it in Activity E
#Override
public void onBackPressed(){
finish() ; // ActivityE.this.finish()
super.onBackPressed() ;
}

Related

Going back two activities without losing top activity

I have 5 activities A, B, C, D and E ,I want to go back from activity E to activity C without losing activity A and activity B.
I want to keep activity A as top activity and keep all data in activity C, which come from activity A and activity B.
Which flags should I use ?
How to implement going back two activities without losing top activities.
For that you should have to understand whole scenario of Android Activity launchMode and set flag programmatically
I Ignore Other things, If you gona go back to C.
Activty D-> Activty E
Intent intent=new Intent(Activty D , Activty E)
startActivty(intent)
finish();
On Press Back Button
#overide onBackPressed()
{
super.onBackPressed();
yourActivity.this.finish; //It will remove E from stack if you want
}
You want to go from Activity E to C , you don't want to back Activity D
A -> B -> C -> D -> E
When you are going to D -> E (D to E) finish Activity D. So when you back from E directly you come to Activity C.
Reference Code :
Intent intent = new Intent(Activity_D.this, Activity_E.class);
finish(); //Kill the activity from which you will go to next activity
startActivity(intent);

How to control/modify the activity stack in my case

I have following activity stack: A->B->C->D
In activity D, a new Activity E will be launched. Is that possible to change the activity stack to A->E (so if user press back they will go to activity A)?
Additional information: I don't want D,E depends on A. So override E's onBackPressed to start A directly or start A from D with CLEAR_TOP flag and then go to E is not a choice.
ok as per your requirement it is possible using startactivityforresult and onActivityResult let me explain
1.first call A to B
2.from B call startactivityforresult C, also in B add onActivityResult to handle result
3.from C call D as step 2.
4. Now when you call E from D just finish D and setResult(RESULT_OK)
6.Now in activity C onActivityResult get called there check for result ok and finish activity and setResult(RESULT_OK) same for B
here you get stack A to E.
for more info http://developer.android.com/training/basics/intents/result.html
if any problem let me know
You can manage both your stack and your back pressed buttons by overriding the onBackPressed method and by using Tasks and Back Stack or not.
So within backpressed methods:
#Override
public void onBackPressed() {
startActivity(new Intent(this, YourChosenClass.class));
super.onBackPressed();
}
A task is a cohesive unit that can move to the "background" when users begin a new task or go to the Home screen, via the Home button. While in the background, all the activities in the task are stopped, but the back stack for the task remains intact—the task has simply lost focus while another task takes place, as shown in figure 2. A task can then return to the "foreground" so users can pick up where they left off. Suppose, for example, that the current task (Task A) has three activities in its stack—two under the current activity.
Activities linked with a task will be available in the back stack.

How an activity go back to a fragment

There is an Activity A including two fragment B and C.
And there are also two Activities D and E.
Now I click a button in C to go D, and click another button in D to go E.
C -> D -> E.
There is no problem.
Now I'm in Activity E, And I want to go back to D then go back to C.
E -> D -> C. There is something wrong.
The order is E->D->E->D...when I click the back button.
All Activities I used call finish().
How can I get the right order E -> D -> C?
You shouldn't use finish() if you want to go back to that activity. If you look at the activity lifecycle, finish() calls the onDestroy(). Check here http://developer.android.com/reference/android/app/Activity.html
With regards to going back to the fragment, look at the documentation and look for addToBackstack() method. This documentation will help with proper navigation: http://developer.android.com/training/implementing-navigation/temporal.html
Please let me know if you battling with anything

Remove Activities from Stack History in Android

So ..
let's Suppose the Following Sequence of Activities
A -> B -> C -> D -> E
if I do The Action1 in E I just want remove E from stack and go Back To D.
on the Other hand, If I do The Action2 in E, I want to remove E and D from stack and return to C
how to do that ?
the above sequence is simple implement of messaging App, so A is The Log in Activity and B is The Profile Activity and C show the Friend Request List and D show the Profile of selected person form C, and in C there are 2 button one for approved and the second for cancel requset, now if click any of them it take him to E where Yes or No to do the Opperation, if No it return to profile of Selected person , of yes it should return to C
Well what you can do is that when you go from A->B call finish(); to end activity A, same for B->C, that is end B. So by the time you reach E, only E will be active. So now when you do Action1, call Activity D, and when you do Action2 call activity C. And this time call finish(); on E. This would do exactly what you want.
Every time when you move from one Activity to another you can add this flag with the intent Intent.FLAG_ACTIVITY_CLEAR_TOP. This will clear your all previous activities so that in Activity E there will be no Activity in Stack.
If it always the last activity who is going to perform this kind of action then you can start that last activity for result. So when D is spawning E, do it using startActivityForResult (Intent intent, int requestCode). And later when you perform actions in E, call finish() for E and before doing that pass a result code to D. Based on that result you can either do nothing (so D will remain as it is) or you can call finish() on D as well.
if by stack u mean recent application then to clear recent application you can add
android:excludeFromRecents="true"
inside your activity tag in manifest.xml
If Action1 do nothing since if a user press the Back button, E will be removed and you are back to D. If Action2, start C with flag Intent.FLAG_ACTIVITY_CLEAR_TOP, the D and E would be cleared from the stack

Problems with back and go to next activity in android

I use intent to go to other Activity
This is my way: Activity A -> B -> C -> D -> E.
When i press Back, it go E -> D -> C-> B-> A
but, in Activity E, when press Back, i want to come back to C, so i use
mIntent.setClass(E.this, C.class);
startActivity(mIntent);
My problems is: When i come to C from E, i press back, it come back to E. But i want to come back to B like C-> B-> A.
In my opinion, when i use above code, i create new Activity C, so i cant come back to Activity B
How can i solve it?
Thanks you so much
Call finish() on D when you move to E, this will remove them from the stack and cause E to go to C. Your stack will look like E, C, B, A because D is removed.
try adding this to your your AndroidManifest.xml file, android:noHistory="true" attribute in D activity tag

Categories

Resources