Note that there are some explanatory texts on larger screens.

plurals
  1. PONested Fragment Backstack places new fragment below instead of replacing
    primarykey
    data
    text
    <p>I have strange behavior happening when I try to use the backstack of nested fragments in My app.</p> <p>Say I have this fragment, FragmentA, and it is placed programatically inside of another rootFragment with a linearlayout designed to hold fragments.</p> <p>rootFragment's layout contains:</p> <pre><code> &lt;LinearLayout android:id="@+id/map_fragmentHolder" android:layout_height="0dp" android:layout_width="match_parent" android:orientation="vertical" android:layout_weight="1.5" /&gt; </code></pre> <p>I use my switchToFragment method (outlined below) to replace FragmentA with a fragment of type FragmentB.</p> <pre><code>public void switchToFragment(Fragment fragment, boolean addtoBackStack) { //Perform the fragment switch FragmentTransaction childTransaction = rootFragment.getChildFragmentManager().beginTransaction(); childTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE | FragmentTransaction.TRANSIT_FRAGMENT_OPEN); childTransaction.replace(R.id.map_fragmentHolder, fragment); if(addtoBackStack){ if(currentFragment != null) fragmentStack.push(currentFragment); childTransaction.addToBackStack(null); } currentFragment = fragment; childTransaction.commit(); } </code></pre> <p>This works perfectly when the fragment replacing the currentFragment is of different types. However for some strange reason, if I try to switch in another fragment of type FragmentB when there is one currently added, the back stack suddenly stops working. Instead of replacing all fragments within the linearlayout, the old fragment is placed below the current one.</p> <p>Here is how I call the back stack to pop. It is done this way because the onBackPressed() method does not handle backstacks on nested fragments by default.</p> <pre><code>public boolean onBackPressed() { if(fragmentStack.size() == 0) return false; //ChildFragmentManagers don't pop the backstack automatically, so we gotta do it manually... rootFragment.getChildFragmentManager().popBackStack(); currentFragment = fragmentStack.pop(); return true; } </code></pre> <p>I have managed to make my app work if I simply replace my onBackPressed method with the following, however I would really like to know what is happening, and why it only happens when I am replacing the fragment with one of the same type.</p> <pre><code>public boolean onBackPressed() { if(fragmentStack.size() == 0) return false; switchToFragment(fragmentStack.pop(), false); return true; } </code></pre> <p>Any ideas or insights would be greatly appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload