How an activity go back to a fragment - android

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

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

Android - Pop two or more fragments but don't the top

I have the following scenario:
A(Bottom) -> B -> C -> D(Top) -back-> A
A(Bottom) -> B -> C -back-> B -back-> A
Once I'm on the top (D fragment) I want to pop B and C fragment. On this way when I press back from D I can to A.
I want to avoid the transitions on B and C when I back from D.
A(Bottom) -> B -> C -> D(Top) -back-> C -back-> B -back-> A
It is even posible?
If I understand your situation correctly, then when creating fragment D right before that you can try getFragmentManager().popBackStack() which will look at your fragment stack and simply pop the last fragment. For this to work, you need to ensure that B and C are on the backstack so when creating those fragments you will need to call addToBackStack(null). Now, when creating fragment D you can just popBackStack() twice and that will bring you back to A. I hope I am understanding your situation correctly.

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

Android community from activities

I have a problem with activities. I have a 4 activities. First is menu, second and thirth are other menus and fourth is screen with help. In every for 3 menu screens I have a button which start activity help. In help activity I have a info about application and button. I want to this button when user use it he back to last active activity. For better understanding: when I am in first menu and click help button, activity "help" is starting and when I click button "back", activity "menu1" is starting. Algorithm must be the same for other menus. Can you help me?
Normally, if you start activity Y from activity X, when activity Y finishes, the control automatically returns to activity X.
Thus, let's say you have three menu activities A, B, C and help activity D. Then, if you do A -> D, then when D finishes, you'll be back on A. If your flow is A -> B -> D, then after D finishes, you should end up back on B. Lastly, if your flow is A -> B -> C -> D, then upon finishing of D, you should come back to C. This is something Android should do for you automatically without you having to code anything special.
Or did I misunderstood your requirements?

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