From 15c21ffad983afdcd69c55ed1a2b40d2c281892d Mon Sep 17 00:00:00 2001 From: George Mount Date: Wed, 19 Jul 2017 10:47:48 -0700 Subject: [PATCH] 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 --- core/java/android/app/Fragment.java | 8 +++++++- core/java/android/app/FragmentManager.java | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/core/java/android/app/Fragment.java b/core/java/android/app/Fragment.java index 66dc6a193af1b..93773454424e7 100644 --- a/core/java/android/app/Fragment.java +++ b/core/java/android/app/Fragment.java @@ -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 diff --git a/core/java/android/app/FragmentManager.java b/core/java/android/app/FragmentManager.java index 7b6b4b49dbc08..ba5ea21847bca 100644 --- a/core/java/android/app/FragmentManager.java +++ b/core/java/android/app/FragmentManager.java @@ -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);