case 1: I have 3 fragments A,B,C. When I am moving from A->B->C and then by pressing a save button on C fragment , I come back to fragment B and now when I press the back button at this point (in fragment B), it takes me to fragment C (C fragment is stored in the backstack) but I need to take the user to fragment A instead of C.
---> I am using this below code when user presses on back button of fragment B.
navController!!.popBackStack()
case 2: When I am moving A->B and now if I press back button it takes me back to fragment A. (This is working fine) . But in above case (case 1), it takes the user to fragment C instead of A.
How should I handle this backstack case?
call same code
navController!!.popBackStack()
after saving button click and before redirection to B fragment
so in C Fragment code will be
//save button clicked
// you logic and code
navController!!.popBackStack()
//navigate back to B fragment
navController!!.navigate(B fragment ID)
Related
Fox example, I have activities A, B, C. Each of these activities is a container for fragments F1, F2, F3. Also, I have another fragment - F4. This fragment which can be opened from fragments F1, F2, F3. In addition, from fragment F4 I need to perform navigation to activities A B and C and also remove from back stack fragment F4. Now I close the F4 fragment for this and using the fragment result API I pass the result to the previous fragment, and from the previous fragment I perform the navigation to the activity I need.
But in this case, the previous fragment appears on the screen for a few seconds, but I would like it to look like I navigated from fragment F4.
Is it possible to avoid this behavior ?
Please, help me ?
I have a view-pager with two fragment in it . based on my logic i want to replace the fragment on a specific position with another fragment but when user back-press i want to go back to previous fragment i replaced with it
for clearance
i have 4 fragment A, B, C, D initially i added A and B to view-pager, then i replaced B with c in view-pager , So that i want is to go back to fragment B (replaced by C) from C (current) while pressing back button instead of going back to another activity
you can do using assign constant variable to your fragment or assign tag at time of Fragment Call.
I manage using VariableName CurrentFragment which update value on Fragment Lunch. Then after onBackpress method of your activity i handle the fragment based on Constant Variable.
Visit below Stackoverflow link where i put solution step-wise to handle multiple fragment on backpress.
Click Here to View StackOverFlowAnswer
I have several fragments that share the same container: Fragment A, Fragment B, Fragment C. I move through them with the .replace method and I always use .addToBackStack to keep them in the stack.
At the current moment, if Fragment C is displayed and I click the Back Button to go back to Fragment B the methods from onCreateView() of Fragment B onwards will be called, but not onAttach or onCreate. I would like to restart Fragment B completely after I click the Back Button so that these methods are also called. In other words, as if I had gone from Fragment B to a different Activity and then back to Fragment B.
I have a small layout in my activity that I add Fragments to based on the User navigating through the app.
Assuming the user navigates thusly:
Activity -> Fragment A -> Fragment B -> Fragment C -> Button Click
I would like to be able to to hide the Fragments and show the blank Activity again.
This is how I'm adding the Fragments to the activity:
protected void addFragment(Fragment fragment)
{
getSupportFragmentManager().beginTransaction().replace(R.id.secondary_fragment, fragment).addToBackStack(fragment.getTitle()).commit();
}
To clear all the Fragments, I use:
getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
However, is there a way to clear the fragments in a way that if the user presses back, they would be able to go back to Fragment C (as opposed to exiting the App)?
Maybe instead of pop all the backStack, you just get the fragment view by id and setVisibility to invisible?
Try starting a new instance of your Activity with a clear stack on the button press (if I'm correct in assuming this comes after C as you described). This way the First Activity instance will still have up to Fragment C and the Second Activity instance will be whatever you like (Fragment A > Fragment D > Fragment F). And you won't need to pop/clear any back stack for any Activity.
HTHs
I've noticed a strange behaviour when not adding a framgent transaction to the back stack. I have 4 fragments: A, B, C and D. The transaction for the C fragment isn't added to the back stack. First I add A, then B, then C. Then I press BACK. A is displayed, which is normal. Then I add D and I press BACK. The result is that the fragment C is displayed. Is there something I am missing, because I would expect C to be out of the back stack and to see fragment A.
The transaction for the C fragment isn't added to the back stack.
ok.
First I add A, then B, then C. Then I press BACK. A is displayed,
If you have added B to the backstack, it should display B and not A.
Then I add D and I press BACK.
If you have not added C, then it should show B and not C. If it is showing C that means that you have added C to the backstack.
Is there something I am missing.
From my understanding of the situation, you have not added B to the backstack and you have added C to the backstack.