Say I have an activity stack as such:
A -> B -> C -> D
I would like to create a new activity "E", and end up with a stack like this:
A -> B -> E
Basically, upon hitting the back button in activity D, I need to swap out activity C for activity E, and land on it.
Is this possible? Or should I be structuring the navigation differently somehow? Thanks!
so while going from activity C to D finish(); C activity.
At last at backPress method of Activity D call activity E and finish(); Activity D
Related
I have 5 activities (A, B, C and D).
A -> B -> C -> D
On activity B and C, if we press the return button, I would like the previous activity to be called (respectively A and B).
Activity D is an end activity without a return button.
When I use "finish ()", it's currently going back to Activity C.
I would like to go back directly to activity A when we reach activity D by executing finish() without going through B and C.
However since B I must always be able to return to A and too C can back to B.
"A" is an activity in launchMode singleTop.
android: noHistory = true on activity D does not allow me to return directly to A.
The only solution I have found for the moment is to use startActicity (A) instead of "finish ()" but I find that this solution is not a good pararique.
Do you have a solution to my problem?
In your D activity use this:
Intent i = new Intent(this, YourFirstActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
This way, you'll return exactly to your A 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);
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
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() ;
}
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