Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling DialogFragment's show() from within onRequestPermissionsResult() causes IllegalStateException in Marshmallow

Steps:

  1. Request a permission from Fragment or Activity
  2. Show a DialogFragment from within onRequestPermissionsResult()
  3. java.lang.IllegalStateException is thrown: Can not perform this action after onSaveInstanceState

This doesn't happen when I show dialog after some delay(using postDelayed). According to http://www.androiddesignpatterns.com/2013/08/fragment-transaction-commit-state-loss.html) on post-Honeycomb devices we CAN commit() between onPause() and onStop() without any STATE LOSS or EXCEPTION. Here is a link to sample project source, log file and issue recorded. https://drive.google.com/folderview?id=0BwvvuYbQTUl6STVSZF9TX2VUeHM&usp=sharing

Also I have opened an issue https://code.google.com/p/android/issues/detail?id=190966 but it was marked as WorkingAsIntended and they suggest to just catch exception. But this doesn't solve the issue. I know other ways to solve it, but isn't this android bug?

UPDATE Status fo the bug is again "assigned". Hope it will be fixed soon. My temp solution is

new Handler().postDelayed(new Runnable() {     @Override     public void run() {         // do your fragment transaction here     } }, 200); 
like image 348
android_dev Avatar asked Oct 21 '15 16:10

android_dev


1 Answers

The bug is accepted and will be fixed, however, I strongly disagree with the postDelayed and timer solutions. The best way to do this would be introducing a state flag in the Activity, in which you set in the callback, and use in onResume or similar. For example:

private boolean someFlag;  public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { // some code checking status     someFlag = true; } 

And then in onResume:

protected void onResume() {     if(someFlag == true) {         doSomething();         someFlag = false;     } } 
like image 184
Kenneth Avatar answered Oct 02 '22 15:10

Kenneth