Short Version:
I have 5 fragments all added to a container by replace method(A -> B -> C -> D -> E
), now I want to remove 3rdfragment from my backStack(C) and stay in the current page, how to do it?
The problem:
I have 3 fragments, FragmentA
, FragmentB
, and FragmentC
, and user navigates from A to B then C
.
In FragmentC when user performs an action (I call it ActionX
) I need to remove FragmentB
from my fragment manager BackStack
(while user still is in FragmentC) and I don't want user to notice anything, just when he presses back on phone different fragment shows (so if ActionX happened when user pressed back fragmentA shows, if not FragmentB shows)
This is how I show my fragments
(I set fragment name for name and tag to find them when I need)
public void showFragment(Fragment fragment) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
String tag = fragment.getClass().getSimpleName();
fragmentTransaction.replace(R.id.fragment_container, fragment, tag);
fragmentTransaction.addToBackStack(tag);
fragmentTransaction.commit();
}
I tried solutions mentiond in some SO threads (like this) but they only work on top fragment in stack or they suggest to not FragmentB (which I cant because I don't know what will user do), also finding and removing FragmentB from stack does not work. One solution is to override my Activity's onBackPressed and check if ActionX happend and pop my backStack twice.
what I tried:
- this don't work (fragmentBInStack is not null but when I remove it nothing happens)
val fragmentBInStack = manager.findFragmentByTag(FragmentB::class.java.simpleName)
fragmentBInStack?.let {
manager.beginTransaction().remove(it).commit()
}
- this code removes both FragmentC and FragmentB and shows FragmentA (I want to stay in FragmentC) if I user 0 instad of
POP_BACK_STACK_INCLUSIVE
it only pops FragmentC and showsFragmentB
manager.popBackStack(FragmentB::class.java.simpleName, FragmentManager.POP_BACK_STACK_INCLUSIVE)
this is some sample code of what I want to do
class FragmentC : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.findViewById<View>(R.id.action_x).setOnClickListener {
//here I want to remove FragmentB from back Stack and stay in FragmentC, just later when user presses back I want to show FragmentA
val manager = activity!!.supportFragmentManager
val fragmentBInStack = manager.findFragmentByTag(FragmentB::class.java.simpleName)
fragmentBInStack?.let {
manager.beginTransaction().remove(it).commit()
}
}
}