Removing a fragment and then re-adding it
<p>I've written a method, <code>setLoading</code> in my activity that is responsible for setting the "loading" status of my app. This method is responsible for instantiating a <code>LoadingFragment</code>, removing any existing instances of it (Using <code>FragmentManager</code>) and then depending on it's first parameter <code>loading</code>, adding it to one of two possible containers (Depending on the <code>top</code> parameter).</p>

<pre><code>protected LoadingFragment loadingFragment;

public void setLoading(boolean loading, boolean top) {
    FragmentManager fragmentManager = getFragmentManager();

    // Create LoadingFragment instance if it has not already been created
    if (loadingFragment == null || !(loadingFragment instanceof LoadingFragment)) {
        loadingFragment = new LoadingFragment();
    }

    // Remove the fragment first if it is present
    fragmentManager
            .beginTransaction()
            .remove(loadingFragment)
            .commit();

    // Only if loading is true should we display the fragment
    if (loading) {
        // Decide which container we're going to put the fragment in
        int id = top ? R.id.topContainer : R.id.container;

        // Place the fragment in the right position
        fragmentManager
                .beginTransaction()
                .add(id, loadingFragment)
                .commit();
    }
}

public void setLoading(boolean loading) {
    setLoading(loading, true);
}
</code></pre>

<p>I am triggering <code>setLoading(true)</code> from elsewhere in my activity and I have commented out it's corresponding <code>setLoading(false)</code> while testing.</p>

<p>What I want to happen is for my <code>LoadingFragment</code> to appear every time <code>setLoading(true)</code> is called. The first call shouldn't remove anything since it at that point it doesn't exist. All subsequent calls should remove the existing <code>LoadingFragment</code> and add it again.</p>

<p>What happens is that the first call to <code>setLoading(true)</code> does indeed create the <code>LoadingFragment</code> and put it in the correct container. However, subsequent calls to <code>setLoading(true)</code> remove the fragment, but it never seems to be re-added. I have checked to see that the fragment does indeed exist and is of type <code>LoadingFragment</code> at the point it is added and I have also checked to ensure that it's <code>onCreateView</code> method is being called.</p>

<p>Am I doing something wrong?</p>

<p><strong>Edit</strong></p>

<p>Using the answer given below by H Raval as a base I have now come up with the following:</p>

<pre><code>public void setLoading(boolean loading, boolean top) {
    FragmentManager fragmentManager = getFragmentManager();

    Fragment currentLoadingFragment = fragmentManager.findFragmentById(R.id.loadingFragment);
    if (currentLoadingFragment != null) {
        fragmentManager
                .beginTransaction()
                .remove(currentLoadingFragment)
                .commit();
    }

    if (loading) {
        int id = top ? R.id.topContainer : R.id.container;

        fragmentManager
                .beginTransaction()
                .add(id, new LoadingFragment())
                .commit();
    }
}
</code></pre>

<p>This seems to work as expected. It seems that the primary difference is that this code is creating a new <code>LoadingFragment</code> instance each time (When <code>loading = true</code>) whereas originally I was trying to use the same instance and just adding/removing it using the <code>FragmentManager</code>.</p>

<p>Out of interest, is there a reason I need to create a new instance after using <code>remove</code>? Is this the correct way to do it? Or should it still work when using the same instance? Additionally, if it's recommended to create a new instance each time, is there anything I should do in terms of clean-up, freeing up resources etc. (Perhaps there's a way of gracefully destroying the obsolete instances)?</p>