I have 3 fragments in my app: [a]->[b]->[c]. first fragment a is added, then it is replaced with b and then b is replaced with c. when the user is in fragment b he can go back to fragment a by pressing back button, but when in fragment c, fragments a and b must not be accessible with back button (pressing back button must close all fragments).
I have tried using
getSupportFragmentManager().popBackStackImmediate(name, FragmentManager.POP_BACK_STACK_INCLUSIVE);
but it pops all fragments from the top (current fragment) to the specified fragment which is not good idea. any simple solution?!
Related
Imagine i have a frameLayout in my main activity, and two fragments,
so now first fragment is being displayed, i replaced it with secondFragment and added first one to back stack, now some condition is met in the secondfragment so i want to close the second fragment and open the first one where we left that (basically back from the back stack) without pressing the back button. how do i achieve this ?
you can write this code for removing current fragment from backstack:
getActivity().getSupportFragmentManager().popBackStack();
I am having a situation Fragment A is used Add but in FragmentB I used replace for FragmentC.
FragmentA--Add---FragmentB---Repace --FragmentC
When i clicked back from fragment C it brings me back to Fragment A.
Note : all fragments added in backstack
Required behavior : When pressed back from fragmentC it must come back to FragmentB. Instead it brings back to fragmentA
Plz help.
Regards
I'm developing an Android application with a BottomBar which is consisted of 4 tabs which are all fragments, let's say A, B, C, D. While being at tab A, the user can press buttons which will open new fragments like A2, A3 etc. My problem is: after I reach A2, when I press on other tabs on the bottom bar (B,C,D), and then press back A, I turn back to fragment A instead of A2.
Is there any solution to save the current fragment (A2) when I switch back to tab A? Or shall I use seperate activities for each tab instead of fragments? I am using the replace() method of the FragmentTransaction class in order to switch between A and A2.
I'd try implementing your A, B, C, D fragments in a ViewPager, then override setUserVisibleHint() on fragment A, which is called by the ViewPager when visibility changes, to save/restore the state according to the visibility arg that's passed in there.
This was asked in one of the interviews. The scenario is described as follows :
MainActivity (initially has Fragment A added to it.). On pressing a button, it goes to Fragment B, without adding A to backstack. On pressing a button in Fragment B, it adds 'B' to back stack, and goes to Fragment C. Now, in fragment C, I have a button through which I directly want to navigate to fragment A (which wasn't added to back stack). Is there any way possible to do this?
I have an activity. When it starts it loads Fragment A in onCreate using replace. This is not added to the back stack.
I then want Fragment B, an overlay, to be added on top of the previous fragment. This is because i still want you to still be able to see Fragment A behind it as Fragment B has a translucent background. This is loaded using add and is added to the back stack.
From Fragment B i load Fragment C using replace and add to the back stack. This is because Fragment C is not an overlay like Fragment B. When i'm in Fragment C i want to go back to Fragment B on back press. I override on back press and pop the back stack.
The issue is when i press the back button i see Fragment A and not Fragment B. Even though i have debugged and can see Fragment B in the back stack. Why is this?