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

This commit is contained in:
TreeHugger Robot
2017-07-19 22:15:24 +00:00
committed by Android (Google) Code Review
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);