当用户单击“运行时权限"对话框上的“拒绝"时显示DialogFragment [英] Show DialogFragment when user clicks deny on Runtime permissions dialog

查看:132
本文介绍了当用户单击“运行时权限"对话框上的“拒绝"时显示DialogFragment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在用户单击android 6的允许/拒绝权限"对话框上的拒绝"后立即显示DialogFragment,但我却收到此错误:

I'm trying to show a DialogFragment right after the user clicks "Deny" on the "allow/deny permission" dialog of android 6 But instead I'm getting this error:

    Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
  at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1448)
  at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1466)
  at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:634)
  at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:613)
  at android.support.v4.app.DialogFragment.show(DialogFragment.java:139)
  at android.support.v4.app.FragmentActivity.onRequestPermissionsResult(FragmentActivity.java:802)
  at android.app.Activity.dispatchRequestPermissionsResult(Activity.java:6553)
  at android.app.Activity.dispatchActivityResult(Activity.java:6432)
  at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742) 
  at android.app.ActivityThread.-wrap16(ActivityThread.java) 
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393) 
  at android.os.Handler.dispatchMessage(Handler.java:102) 
  at android.os.Looper.loop(Looper.java:148) 
  at android.app.ActivityThread.main(ActivityThread.java:5417) 
  at java.lang.reflect.Method.invoke(Native Method) 
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

我的对话代码非常标准

  protected void showDialog(DialogFragment diag)
    {
        if (diag != null && !diag.isAdded())
            diag.show(getSupportFragmentManager(), "dialog");
    }

如何解决?

推荐答案

因此,有两种解决方案,没有一个特别漂亮.这意味着您的Activity的FragmentManager尚未处于以安全方式提交FragmentTransactions的有效状态(尽管在这种情况下,应该始终如此).

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)使用.commitAllowingStateLoss()代替.show():

这是最简单的解决方法,尽管我尚不完全清楚不使用.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)将.show()作为Runnable张贴在Handler上:

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)设置一个标志并在onResume()中显示对话框

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
    }
}

这篇关于当用户单击“运行时权限"对话框上的“拒绝"时显示DialogFragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆