From 0eb826e8c5e0ac22c550c5846a913930b5063638 Mon Sep 17 00:00:00 2001 From: Sam Stern Date: Mon, 2 Oct 2017 15:49:50 -0700 Subject: [PATCH] Fix fragment states Change-Id: Iae9ee2791a3d2f408aef699028212c5bada7eb68 --- .../RationaleDialogFragment.java | 27 +++++++++++++++++++ .../RationaleDialogFragmentCompat.java | 13 +++++++++ .../BaseFrameworkPermissionsHelper.java | 2 +- .../helper/BaseSupportPermissionsHelper.java | 2 +- 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/easypermissions/src/main/java/pub/devrel/easypermissions/RationaleDialogFragment.java b/easypermissions/src/main/java/pub/devrel/easypermissions/RationaleDialogFragment.java index 7bf78e8..9cb55e0 100644 --- a/easypermissions/src/main/java/pub/devrel/easypermissions/RationaleDialogFragment.java +++ b/easypermissions/src/main/java/pub/devrel/easypermissions/RationaleDialogFragment.java @@ -2,6 +2,7 @@ import android.app.Dialog; import android.app.DialogFragment; +import android.app.FragmentManager; import android.content.Context; import android.os.Build; import android.os.Bundle; @@ -19,6 +20,7 @@ public class RationaleDialogFragment extends DialogFragment { public static final String TAG = "RationaleDialogFragment"; private EasyPermissions.PermissionCallbacks mPermissionCallbacks; + private boolean mStateSaved = false; public static RationaleDialogFragment newInstance( @StringRes int positiveButton, @StringRes int negativeButton, @@ -47,6 +49,31 @@ && getParentFragment() instanceof EasyPermissions.PermissionCallbacks) { } } + @Override + public void onSaveInstanceState(Bundle outState) { + mStateSaved = true; + super.onSaveInstanceState(outState); + } + + /** + * Version of {@link #show(FragmentManager, String)} that no-ops when an IllegalStateException + * would otherwise occur. + */ + public void showAllowingStateLoss(FragmentManager manager, String tag) { + // API 26 added this convenient method + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + if (manager.isStateSaved()) { + return; + } + } + + if (mStateSaved) { + return; + } + + show(manager, tag); + } + @Override public void onDetach() { super.onDetach(); diff --git a/easypermissions/src/main/java/pub/devrel/easypermissions/RationaleDialogFragmentCompat.java b/easypermissions/src/main/java/pub/devrel/easypermissions/RationaleDialogFragmentCompat.java index 6da908a..c292872 100644 --- a/easypermissions/src/main/java/pub/devrel/easypermissions/RationaleDialogFragmentCompat.java +++ b/easypermissions/src/main/java/pub/devrel/easypermissions/RationaleDialogFragmentCompat.java @@ -6,6 +6,7 @@ import android.support.annotation.NonNull; import android.support.annotation.RestrictTo; import android.support.annotation.StringRes; +import android.support.v4.app.FragmentManager; import android.support.v7.app.AppCompatDialogFragment; /** @@ -34,6 +35,18 @@ public static RationaleDialogFragmentCompat newInstance( return dialogFragment; } + /** + * Version of {@link #show(FragmentManager, String)} that no-ops when an IllegalStateException + * would otherwise occur. + */ + public void showAllowingStateLoss(FragmentManager manager, String tag) { + if (manager.isStateSaved()) { + return; + } + + show(manager, tag); + } + @Override public void onAttach(Context context) { super.onAttach(context); diff --git a/easypermissions/src/main/java/pub/devrel/easypermissions/helper/BaseFrameworkPermissionsHelper.java b/easypermissions/src/main/java/pub/devrel/easypermissions/helper/BaseFrameworkPermissionsHelper.java index 1eff27c..fc88966 100644 --- a/easypermissions/src/main/java/pub/devrel/easypermissions/helper/BaseFrameworkPermissionsHelper.java +++ b/easypermissions/src/main/java/pub/devrel/easypermissions/helper/BaseFrameworkPermissionsHelper.java @@ -24,6 +24,6 @@ public void showRequestPermissionRationale(@NonNull String rationale, @NonNull String... perms) { RationaleDialogFragment .newInstance(positiveButton, negativeButton, rationale, requestCode, perms) - .show(getFragmentManager(), RationaleDialogFragment.TAG); + .showAllowingStateLoss(getFragmentManager(), RationaleDialogFragment.TAG); } } diff --git a/easypermissions/src/main/java/pub/devrel/easypermissions/helper/BaseSupportPermissionsHelper.java b/easypermissions/src/main/java/pub/devrel/easypermissions/helper/BaseSupportPermissionsHelper.java index 40eeeda..fe3e407 100644 --- a/easypermissions/src/main/java/pub/devrel/easypermissions/helper/BaseSupportPermissionsHelper.java +++ b/easypermissions/src/main/java/pub/devrel/easypermissions/helper/BaseSupportPermissionsHelper.java @@ -24,6 +24,6 @@ public void showRequestPermissionRationale(@NonNull String rationale, @NonNull String... perms) { RationaleDialogFragmentCompat .newInstance(positiveButton, negativeButton, rationale, requestCode, perms) - .show(getSupportFragmentManager(), RationaleDialogFragmentCompat.TAG); + .showAllowingStateLoss(getSupportFragmentManager(), RationaleDialogFragmentCompat.TAG); } }