Sorry I didnβt add the code, but I really didnβt know which part could do this. But I finally realized the reason.
In the onCreate method of parent activity, I created a fragment by calling
if(mFragment == null) { mFragment = onCreatePane(); getSupportFragmentManager() .beginTransaction() .add(R.id.root_container,mFragment) .commit(); }
I thought the test for null sufficient. but the contents of the fragment remain in FrameLayout either after onDestroy is called in Activity (by turning the device). This is strange, isn't it?
I tried putting this code in onStop()
if(mFragment != null) { getSupportFragmentManager() .beginTransaction() .remove(mFragment) .commit(); }
but the result was the same. The only solution I found was to replace the add() method with the replace() method. But does anyone know why removing the fragment did not help? And why does the fragment content remain in FrameLayout after the Activity is destroyed?
Thanks again.
EDIT:
Now I know what the problem is. I have to check savedStateInstance == null instead of mFragment == null . If I do not, the main action will restore the old fragment from the saved state, and it will also create a new fragment in my onCreate method.
source share