Skip to content
This repository was archived by the owner on Oct 3, 2024. It is now read-only.

Fix fragment states #170

Merged
merged 1 commit into from
Oct 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}