Problems with back and go to next activity in android - 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

Related

Go back to different Activity in back stack in Android

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

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

Removing an Activity from History Stack [duplicate]

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() ;
}

clear back stack of activities

I have some activities A, B, C, D. No the way it's set up is like this.
When user starts the app, activity A starts.
Based on a preference, which if set to true, immediately starts activity D
From there D starts C and C starts B which then starts A
... At this point i want D, C, B removed from the back stack so that user cant go back to them by pressing back from A (but the back button should work like it should when in D, B, C).
so to sum it up i need something like this
D <--> C <--> B --> A
I tried using intent flags Intent.FLAG_ACTIVITY_CLEAR_TOP & Intent.FLAG_ACTIVITY_NEW_TASK
but they dont work.
How do i accomplish this??
Then start activity A with FLAG_ACTIVITY_TASK_ON_HOME flag set.
Ok.
How about calling finish on B after launching A?

Categories

Resources