From 9f5a092b05e65f5cecc6a0008c6caa1cb93f3107 Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 21 Aug 2019 14:01:00 +0200 Subject: [PATCH] Prevent crash --- .../eu/faircode/email/FragmentDialogEx.java | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/app/src/main/java/eu/faircode/email/FragmentDialogEx.java b/app/src/main/java/eu/faircode/email/FragmentDialogEx.java index 5e0ee5aefe..d5a69933fa 100644 --- a/app/src/main/java/eu/faircode/email/FragmentDialogEx.java +++ b/app/src/main/java/eu/faircode/email/FragmentDialogEx.java @@ -2,18 +2,46 @@ package eu.faircode.email; import android.content.DialogInterface; import android.content.Intent; +import android.os.Bundle; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.DialogFragment; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; +import androidx.lifecycle.Lifecycle; +import androidx.lifecycle.LifecycleObserver; +import androidx.lifecycle.OnLifecycleEvent; import static android.app.Activity.RESULT_CANCELED; public class FragmentDialogEx extends DialogFragment { private boolean once = false; + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + Log.i("Create " + this); + } + + @Override + public void onResume() { + super.onResume(); + Log.i("Resume " + this); + } + + @Override + public void onPause() { + super.onPause(); + Log.i("Pause " + this); + } + + @Override + public void onDestroy() { + super.onDestroy(); + Log.i("Destroy " + this); + } + @Override public void onStart() { try { @@ -40,6 +68,20 @@ public class FragmentDialogEx extends DialogFragment { super.onDismiss(dialog); } + @Override + public void setTargetFragment(@Nullable Fragment fragment, int requestCode) { + super.setTargetFragment(fragment, requestCode); + Log.i("Set target " + this + " " + fragment); + + fragment.getViewLifecycleOwner().getLifecycle().addObserver(new LifecycleObserver() { + @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) + public void onDestroy() { + Log.i("Reset target " + FragmentDialogEx.this); + FragmentDialogEx.super.setTargetFragment(null, requestCode); + } + }); + } + protected void sendResult(int result) { if (!once) { once = true;