Skip to content

Commit

Permalink
Fix fragment states (#170)
Browse files Browse the repository at this point in the history
Change-Id: Iae9ee2791a3d2f408aef699028212c5bada7eb68
  • Loading branch information
samtstern committed Oct 2, 2017
1 parent 1b84362 commit bb59909
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
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);
}
}

0 comments on commit bb59909

Please sign in to comment.