From 371a809179c843d7ae661a10bc9b4b8cfcaff566 Mon Sep 17 00:00:00 2001 From: Adam Powell Date: Fri, 20 Jun 2014 12:51:12 -0700 Subject: Inflate fragments from layout into child FragmentManagers Previously, if an app inflated a layout in a Fragment's onCreateView that itself had fragments included, those fragments would be added to the Activity-level FragmentManager and would not share the same lifecycle with the fragment it was inflated for. This led to some nasty management headaches. If an app targets L or above, add the fragment to the child FragmentManager of the current fragment when inflated using the LayoutInflater passed to the parent fragment. Bug 12763389 Change-Id: Iad4ed7d5df602aea9579bf1503e206fa894630c1 --- core/java/android/app/Fragment.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'core/java/android/app/Fragment.java') diff --git a/core/java/android/app/Fragment.java b/core/java/android/app/Fragment.java index 6c0d379..2ff3d57 100644 --- a/core/java/android/app/Fragment.java +++ b/core/java/android/app/Fragment.java @@ -23,6 +23,7 @@ import android.content.Context; import android.content.Intent; import android.content.res.Configuration; import android.content.res.Resources; +import android.os.Build; import android.os.Bundle; import android.os.Parcel; import android.os.Parcelable; @@ -1104,7 +1105,15 @@ public class Fragment implements ComponentCallbacks2, OnCreateContextMenuListene * inflation. Maybe this should become a public API. Note sure. */ public LayoutInflater getLayoutInflater(Bundle savedInstanceState) { - return mActivity.getLayoutInflater(); + // Newer platform versions use the child fragment manager's LayoutInflaterFactory. + if (mActivity.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.L) { + LayoutInflater result = mActivity.getLayoutInflater().cloneInContext(mActivity); + getChildFragmentManager(); // Init if needed; use raw implementation below. + result.setPrivateFactory(mChildFragmentManager.getLayoutInflaterFactory()); + return result; + } else { + return mActivity.getLayoutInflater(); + } } /** -- cgit v1.1