Note that there are some explanatory texts on larger screens.

plurals
  1. POFragment not destroyed on rotation
    primarykey
    data
    text
    <p>I have a fragment which is added programmatically. I have however discovered that after a rotation, a new fragment is created but the old fragment is still alive (though not being displayed) I assumed that fragments are destroyed along with the activity on rotation, but this is not the case here. What could be causing my fragment to be retained / not destroyed? Please note:</p> <ol> <li>I am not setting setRetainInstance.</li> <li>The fragment is not added in the activity onCreate (added later when a button is tapped)</li> <li>I can see onDestroy is being called for the Activity upon rotation</li> <li>Although the fragment is added, it obscures the add button and is only be dismissed when I do a "remove".</li> </ol> <p>Code creating the fragment:</p> <pre><code>MyFragment fragment = MyFragment(); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.add(R.id.content_frame, fragment, "CurrentFragment"); fragmentTransaction.show(fragment).commit(); </code></pre> <p>Code removing the fragment:</p> <pre><code>FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.remove(fragment).commit(); </code></pre> <p>UPDATE: I cannot confirm if this was the problem, but the fragment registers as a LocalBroadcast receiver in onCreate. It unregistered for broadcasts in onDestroy, but it seems that either the LocalBroadcast registration was preventing onDestroy being called, or onDestroy was not called upon rotation. I now unregister in onStop and this seems to have solved the issue.</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.
 

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