Note that there are some explanatory texts on larger screens.

plurals
  1. POFragments: Remove all fragments in a view
    text
    copied!<p>The scenario I am faced with, is in my application I have a <em>single pane</em> and <em>dual pane</em> style layout. Rather than individually handle every single navigation operation possible between the screens, for every different style of layout, I am using a function which sets up the layout correctly when given the desired screen.</p> <p>It is basically a <code>switch</code> statement for each screen in the app, with a nested <code>switch</code> statement in each screen to handle each layout style. This is what I'm talking about in code:</p> <pre><code>protected void setupScreen() { switch(currentScreen) { case SCREEN_ONE: switch(currentLayout) { case SINGLE_PANE: // Perform actions to setup the screen break; case DUAL_PANE: // Perform actions to setup the screen break; } break; case SCREEN_TWO: switch(currentLayout) { case SINGLE_PANE: // Perform actions to setup the screen break; case DUAL_PANE: // Perform actions to setup the screen break; } break // ... etc .... } } </code></pre> <p>In the section where I want to <em>perform the actions to setup the screen</em>, this consists of the following basic three operations:</p> <pre><code>// Create the fragments if necessary if (screenFragment == null) { screenFragment = new myFragment(); } // Remove the existing fragments from the layout views // HOW??? // Add the fragments for this screen to the view getSupportFragmentManager().beginTransaction().add(pane1.getId(), myFragment, "myFragment").commit(); </code></pre> <p>As you can see, what I am struggling with is <strong>how</strong> to do the second step. <strong>How</strong> do you remove all <code>Fragment</code>s from a given <code>View</code> without knowing exactly which ones you are wanting to remove? The closest I have found is <code>FragmentTransaction.replace()</code> which does successfully do this for every case <strong>but</strong> when it turns out you are replacing a <code>Fragment</code> with the same fragment. In this case, it does not remove all, then add (like the documentation suggests), it just seems to remove. Is this an issue with using the compatibility libraries or is it not the way <code>FragmentTransaction.replace()</code> should be used?</p> <p>In any case, how should I go about doing this? Do I have to code a <code>removeAllFragments()</code> function to go through every fragment and detach it or is there a way to do the first half of what the 'two in one' <code>FragmentTransaction.replace()</code> function claims to do?</p>
 

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