Skip to content

Commit

Permalink
Clears Fragment reference in FragmentContextWrapper after Fragment#on…
Browse files Browse the repository at this point in the history
…Destroy()

This CL uses the fragment Lifecycle to clear the fragment instance from the FragmentContextWrapper after onDestroy.

Fixes: #2070

See #2070

RELNOTES=Fixes #2070: clears Fragment reference in FragmentContextWrapper after Fragment#onDestroy()
PiperOrigin-RevId: 352579115
  • Loading branch information
bcorso authored and Dagger Team committed Jan 19, 2021
1 parent 8c45caf commit 1822650
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 5 deletions.
1 change: 1 addition & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ maven_install(
"androidx.appcompat:appcompat:1.2.0",
"androidx.activity:activity:1.2.0-rc01",
"androidx.fragment:fragment:1.3.0-rc01",
"androidx.lifecycle:lifecycle-common:2.3.0-rc01",
"androidx.lifecycle:lifecycle-viewmodel:2.3.0-rc01",
"androidx.lifecycle:lifecycle-viewmodel-savedstate:2.3.0-rc01",
"androidx.multidex:multidex:2.0.1",
Expand Down
1 change: 1 addition & 0 deletions java/dagger/hilt/android/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ gen_maven_artifact(
"androidx.activity:activity",
"androidx.annotation:annotation",
"androidx.fragment:fragment",
"androidx.lifecycle:lifecycle-common",
"androidx.lifecycle:lifecycle-viewmodel",
"androidx.lifecycle:lifecycle-viewmodel-savedstate",
"androidx.savedstate:savedstate",
Expand Down
1 change: 1 addition & 0 deletions java/dagger/hilt/android/internal/managers/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ android_library(
"@maven//:androidx_activity_activity",
"@maven//:androidx_annotation_annotation",
"@maven//:androidx_fragment_fragment",
"@maven//:androidx_lifecycle_lifecycle_common",
"@maven//:androidx_lifecycle_lifecycle_viewmodel",
],
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package dagger.hilt.android.internal.managers;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.LifecycleOwner;
import android.content.Context;
import android.content.ContextWrapper;
import androidx.fragment.app.Fragment;
Expand Down Expand Up @@ -104,7 +107,7 @@ private GeneratedComponentManager<?> getParentComponentManager(boolean allowMiss
if (context instanceof FragmentContextWrapper) {

FragmentContextWrapper fragmentContextWrapper = (FragmentContextWrapper) context;
return (GeneratedComponentManager<?>) fragmentContextWrapper.fragment;
return (GeneratedComponentManager<?>) fragmentContextWrapper.getFragment();
} else if (allowMissing) {
// We didn't find anything, so return null if we're not supposed to fail.
// The rest of the logic is just about getting a good error message.
Expand Down Expand Up @@ -167,20 +170,38 @@ private static Context unwrap(Context context, Class<?> target) {
*/
// This is only non-final for the account override
public static final class FragmentContextWrapper extends ContextWrapper {
private Fragment fragment;
private LayoutInflater baseInflater;
private LayoutInflater inflater;
public final Fragment fragment;

public FragmentContextWrapper(Context base, Fragment fragment) {
private final LifecycleEventObserver fragmentLifecycleObserver =
new LifecycleEventObserver() {
@Override
public void onStateChanged(LifecycleOwner source, Lifecycle.Event event) {
if (event == Lifecycle.Event.ON_DESTROY) {
// Prevent the fragment from leaking if the view outlives the fragment.
// See https://github.com/google/dagger/issues/2070
FragmentContextWrapper.this.fragment = null;
}
}
};

FragmentContextWrapper(Context base, Fragment fragment) {
super(Preconditions.checkNotNull(base));
this.baseInflater = null;
this.fragment = Preconditions.checkNotNull(fragment);
this.fragment.getLifecycle().addObserver(fragmentLifecycleObserver);
}

public FragmentContextWrapper(LayoutInflater baseInflater, Fragment fragment) {
FragmentContextWrapper(LayoutInflater baseInflater, Fragment fragment) {
super(Preconditions.checkNotNull(Preconditions.checkNotNull(baseInflater).getContext()));
this.baseInflater = baseInflater;
this.fragment = Preconditions.checkNotNull(fragment);
this.fragment.getLifecycle().addObserver(fragmentLifecycleObserver);
}

Fragment getFragment() {
Preconditions.checkNotNull(fragment, "The fragment has already been destroyed.");
return fragment;
}

@Override
Expand Down
1 change: 1 addition & 0 deletions java/dagger/hilt/android/testing/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ gen_maven_artifact(
"androidx.activity:activity",
"androidx.annotation:annotation",
"androidx.fragment:fragment",
"androidx.lifecycle:lifecycle-common",
"androidx.lifecycle:lifecycle-viewmodel",
"androidx.lifecycle:lifecycle-viewmodel-savedstate",
"androidx.multidex:multidex",
Expand Down

0 comments on commit 1822650

Please sign in to comment.