Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I restore a previously displayed Fragment?
    primarykey
    data
    text
    <p>I'm using the compatibility package v4 in my project and I'm having an issue with keeping a <code>Fragment</code> around after it's removed from view. I have my <code>Activity</code> displaying 2 fragments...a menu frame on the left, content pane on the right. The menu frame has 3 different menus (Fragments) that can be displayed.</p> <p>This is how I'm replacing the menu Fragment:</p> <pre><code>public void showMenuFragment( Fragment fragment, String tag ) { showFragment( R.id.menu_frame, fragment, tag, false); setLastMenuPushed( tag ); } protected void showFragment( int resId, Fragment fragment, String tag, boolean addToBackStack ) { FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction transaction = fragmentManager.beginTransaction(); if ( fragmentManager.findFragmentByTag( tag ) != null &amp;&amp; fragment.isAdded() ) { transaction.remove( fragment ); } transaction.replace( resId, fragment, tag ).setTransition( FragmentTransaction.TRANSIT_FRAGMENT_OPEN ).setBreadCrumbShortTitle( tag ); if ( addToBackStack ) { transaction.addToBackStack( tag ); } transaction.commit(); } </code></pre> <p>The menu <code>Fragment</code>s require data to be loaded before it can be displayed, so I show a loading spinner. Once it's loaded the first time, I want to not have to ever load it a second time unless the <code>Activity</code> is finished.</p> <p>When I call <code>showMenuFragment(...)</code> I try to pass in a <code>Fragment</code> using <code>FragmentManager.findFragmentByTag(String tag)</code> but it's always <code>null</code>, so I have to make a new <code>Fragment</code> every time I want to show a different menu.</p> <p>My question is, how do I keep these <code>Fragment</code>s around after they are replaced by other menus? I want to avoid keeping manual references to the <code>Fragment</code>s because when the device is rotated, the instance of my <code>Activity</code> class is destroyed and I would lose those references.</p> <p>EDIT: To put in much simpler terms, I want the <code>FragmentManager</code> to keep track of previous <code>Fragment</code>s without having to add them to the back stack. I don't want to hit the back button and see the previous menu shown.</p>
    singulars
    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.
 

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