Fix Fragment.onCreate() not being called in some cases.

Bug 62798751

When an application process has been killed with a retained instance
fragment on its back stack, and a configuration change causes
state save, the retained instance fragment was not calling onCreate()
when it was popped back into existence.

The cause of the problem is that the mRetaining flag was being set,
and when it was supposed to go through onCreate(), it was skipped.
This CL uses a new flag to judge whether or not a Fragment has
called onCreate() or not.

Test: I512f8c174f557ee7ed18a9b9dc5c0c3e7a5843a5
Support Lib Change: Ia66a1f474d815f657b6962097139c796f7fa198b

Change-Id: Ia7878f4ac67ee8cfa05903ba6749ab9851f603ee
This commit is contained in:
George Mount
2017-07-19 10:47:48 -07:00
parent a9f592fc4d
commit 15c21ffad9
2 changed files with 8 additions and 2 deletions

View File

@@ -42,7 +42,6 @@ import android.util.AndroidRuntimeException;
import android.util.ArrayMap;
import android.util.AttributeSet;
import android.util.DebugUtils;
import android.util.Log;
import android.util.SparseArray;
import android.util.SuperNotCalledException;
import android.view.ContextMenu;
@@ -406,6 +405,11 @@ public class Fragment implements ComponentCallbacks2, OnCreateContextMenuListene
// getLayoutInflater()
LayoutInflater mLayoutInflater;
// Keep track of whether or not this Fragment has run performCreate(). Retained instance
// fragments can have mRetaining set to true without going through creation, so we must
// track it separately.
boolean mIsCreated;
/**
* State information that has been retrieved from a fragment instance
* through {@link FragmentManager#saveFragmentInstanceState(Fragment)
@@ -2483,6 +2487,7 @@ public class Fragment implements ComponentCallbacks2, OnCreateContextMenuListene
mState = CREATED;
mCalled = false;
onCreate(savedInstanceState);
mIsCreated = true;
if (!mCalled) {
throw new SuperNotCalledException("Fragment " + this
+ " did not call through to super.onCreate()");
@@ -2759,6 +2764,7 @@ public class Fragment implements ComponentCallbacks2, OnCreateContextMenuListene
}
mState = INITIALIZING;
mCalled = false;
mIsCreated = false;
onDestroy();
if (!mCalled) {
throw new SuperNotCalledException("Fragment " + this

View File

@@ -1232,7 +1232,7 @@ final class FragmentManagerImpl extends FragmentManager implements LayoutInflate
}
dispatchOnFragmentAttached(f, mHost.getContext(), false);
if (!f.mRetaining) {
if (!f.mIsCreated) {
dispatchOnFragmentPreCreated(f, f.mSavedFragmentState, false);
f.performCreate(f.mSavedFragmentState);
dispatchOnFragmentCreated(f, f.mSavedFragmentState, false);