From 3fd812988f0fc47c71f4455d58b020d54fb9a906 Mon Sep 17 00:00:00 2001 From: George Mount Date: Mon, 17 Apr 2017 16:50:26 -0700 Subject: [PATCH] Adds getLayoutInflater() to return a cached value. Bug 37156970 Many applications need to get the LayoutInflater used for fragment View inflation. onGetLayoutInflater() can be heavy weight, so a version that caches that last value is added. Support Lib: I11d719d7416a5bb3b65f37a78e6ed96a69c32332 Test: I6aa0ecfe7136d133d15a597857f6cf6f3cf68fca Change-Id: I686d5b39f7a046396fff124c33c22a73952ee5d7 --- api/current.txt | 1 + api/system-current.txt | 1 + api/test-current.txt | 1 + core/java/android/app/Fragment.java | 37 ++++++++++++++++++++++ core/java/android/app/FragmentManager.java | 4 +-- 5 files changed, 42 insertions(+), 2 deletions(-) diff --git a/api/current.txt b/api/current.txt index 7a3ec57ebcbb7..c435b586db15e 100644 --- a/api/current.txt +++ b/api/current.txt @@ -4577,6 +4577,7 @@ package android.app { method public final android.app.FragmentManager getFragmentManager(); method public final java.lang.Object getHost(); method public final int getId(); + method public final android.view.LayoutInflater getLayoutInflater(); method public android.app.LoaderManager getLoaderManager(); method public final android.app.Fragment getParentFragment(); method public android.transition.Transition getReenterTransition(); diff --git a/api/system-current.txt b/api/system-current.txt index 37bba95394a7c..0298605b518a2 100644 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -4744,6 +4744,7 @@ package android.app { method public final android.app.FragmentManager getFragmentManager(); method public final java.lang.Object getHost(); method public final int getId(); + method public final android.view.LayoutInflater getLayoutInflater(); method public android.app.LoaderManager getLoaderManager(); method public final android.app.Fragment getParentFragment(); method public android.transition.Transition getReenterTransition(); diff --git a/api/test-current.txt b/api/test-current.txt index 02b3db7e4074b..4d6a3476d7b91 100644 --- a/api/test-current.txt +++ b/api/test-current.txt @@ -4590,6 +4590,7 @@ package android.app { method public final android.app.FragmentManager getFragmentManager(); method public final java.lang.Object getHost(); method public final int getId(); + method public final android.view.LayoutInflater getLayoutInflater(); method public android.app.LoaderManager getLoaderManager(); method public final android.app.Fragment getParentFragment(); method public android.transition.Transition getReenterTransition(); diff --git a/core/java/android/app/Fragment.java b/core/java/android/app/Fragment.java index c7bcc54b51b6f..c5b93cd62b4c5 100644 --- a/core/java/android/app/Fragment.java +++ b/core/java/android/app/Fragment.java @@ -509,6 +509,10 @@ public class Fragment implements ComponentCallbacks2, OnCreateContextMenuListene // True if mHidden has been changed and the animation should be scheduled. boolean mHiddenChanged; + // The cached value from onGetLayoutInflater(Bundle) that will be returned from + // getLayoutInflater() + LayoutInflater mLayoutInflater; + /** * State information that has been retrieved from a fragment instance * through {@link FragmentManager#saveFragmentInstanceState(Fragment) @@ -1388,6 +1392,38 @@ public class Fragment implements ComponentCallbacks2, OnCreateContextMenuListene return result; } + /** + * Returns the cached LayoutInflater used to inflate Views of this Fragment. If + * {@link #onGetLayoutInflater(Bundle)} has not been called {@link #onGetLayoutInflater(Bundle)} + * will be called with a {@code null} argument and that value will be cached. + *

+ * The cached LayoutInflater will be replaced immediately prior to + * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)} and cleared immediately after + * {@link #onDetach()}. + * + * @return The LayoutInflater used to inflate Views of this Fragment. + */ + public final LayoutInflater getLayoutInflater() { + if (mLayoutInflater == null) { + return performGetLayoutInflater(null); + } + return mLayoutInflater; + } + + /** + * Calls {@link #onGetLayoutInflater(Bundle)} and caches the result for use by + * {@link #getLayoutInflater()}. + * + * @param savedInstanceState If the fragment is being re-created from + * a previous saved state, this is the state. + * @return The LayoutInflater used to inflate Views of this Fragment. + */ + LayoutInflater performGetLayoutInflater(Bundle savedInstanceState) { + LayoutInflater layoutInflater = onGetLayoutInflater(savedInstanceState); + mLayoutInflater = layoutInflater; + return mLayoutInflater; + } + /** * @deprecated Use {@link #onInflate(Context, AttributeSet, Bundle)} instead. */ @@ -2835,6 +2871,7 @@ public class Fragment implements ComponentCallbacks2, OnCreateContextMenuListene void performDetach() { mCalled = false; onDetach(); + mLayoutInflater = null; if (!mCalled) { throw new SuperNotCalledException("Fragment " + this + " did not call through to super.onDetach()"); diff --git a/core/java/android/app/FragmentManager.java b/core/java/android/app/FragmentManager.java index 91578a2da9c7c..2676f264415dc 100644 --- a/core/java/android/app/FragmentManager.java +++ b/core/java/android/app/FragmentManager.java @@ -1259,7 +1259,7 @@ final class FragmentManagerImpl extends FragmentManager implements LayoutInflate } } f.mContainer = container; - f.mView = f.performCreateView(f.onGetLayoutInflater( + f.mView = f.performCreateView(f.performGetLayoutInflater( f.mSavedFragmentState), container, f.mSavedFragmentState); if (f.mView != null) { f.mView.setSaveFromParentEnabled(false); @@ -1431,7 +1431,7 @@ final class FragmentManagerImpl extends FragmentManager implements LayoutInflate void ensureInflatedFragmentView(Fragment f) { if (f.mFromLayout && !f.mPerformedCreateView) { - f.mView = f.performCreateView(f.onGetLayoutInflater( + f.mView = f.performCreateView(f.performGetLayoutInflater( f.mSavedFragmentState), null, f.mSavedFragmentState); if (f.mView != null) { f.mView.setSaveFromParentEnabled(false);