Show DialogFragment When User Clicks Deny On Runtime Permissions Dialog
Solution 1:
So, there are a couple of solutions to this, none particularly pretty. What it means is that your Activity's FragmentManager is not yet in a valid state for committing FragmentTransactions in a safe manner (although in this particular case, it should always be.)
1) Use .commitAllowingStateLoss()
instead of .show()
:
This is the easiest fix, although I'm not entirely clear what differences arise by not using the .show()
methods, which set a few flags internally. Seems to work fine.
getSupportFragmentManager()
.beginTransaction()
.add(diag, "dialog")
.commitAllowingStateLoss();
1) Post the .show()
as a Runnable
on a Handler
:
// Initialize a Handler ahead of time and keep it around
private Handler mHandler = new Handler();
...
// after getting denied:
mHandler.post(new Runnable() {
@Override
public void run() {
// Show your dialog
}
}
...
@Override
protected void onStop() {
super.onStop();
// Clear out the Runnable for an unlikely edge case where your
// Activity goes to stopped state before the Runnable executes
mHandler.removeCallbacksAndMessages(null);
}
2) Set a flag and show the dialog in onResume()
private boolean mPermissionDenied;
@Override
public void onRequestPermissionsResult(...) {
// If denied...
mPermissionDenied = true;
}
@Override
protected void onResume() {
super.onResume();
if (mPermissionDenied) {
mPermissionDenied = false;
// Show your dialog
}
}
Solution 2:
I will also like to add something I just discovered. If I switch the getSupportFragmentManager()
on my existing code with getSupportFragmentManager().beginTransaction()
it works fine. Code below
protected void showDialog(final DialogFragment diag)
{
cancelDialog();
if (diag != null && !diag.isAdded())
diag.show(getSupportFragmentManager().beginTransaction(), "dialog");
}
No idea why this works as I want to.
Solution 3:
Showing a dialog after the user click on deny is not what guidelines said. You should show the permission rational only before a second request. You can look here for an automatic management: Permission library
Post a Comment for "Show DialogFragment When User Clicks Deny On Runtime Permissions Dialog"