对话框片段中未调用 onRequestPermissionsResult [英] onRequestPermissionsResult not being called in dialog fragment

查看:34
本文介绍了对话框片段中未调用 onRequestPermissionsResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始研究 Android M 运行时权限.在这里,我面临的问题是,如果从 Dialog Fragment 类调用 requestPermissions,则 onRequestPermissionsResult 不会在同一个 Dialog fragment<中调用/代码> 类.但是,如果从 Activity 类或 Fragment 类调用 requestPermissions,则在同一类中调用 onRequestPermissionsResult 方法.

这是我的示例代码:

public class ContactPickerDialog extends DialogFragment {私有静态最终 int READ_CONTACTS_REQUEST_CODE = 12;私有上下文 mContext;私有无效负载接触(){如果(hasPermission(mContext,Manifest.permission.READ_CONTACTS)){新的 ContactSyncTask().execute();} 别的 {this.requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, READ_CONTACTS_REQUEST_CODE);}}@覆盖public void onRequestPermissionsResult(int requestCode, String[] 权限, int[] grantResults) {Logger.d("TAG", "dialog onRequestPermissionsResult");开关(请求代码){案例 READ_CONTACTS_REQUEST_CODE://检查是否授予权限如果(grantResults[0] == PackageManager.PERMISSION_GRANTED){新的 ContactSyncTask().execute();} 别的 {//没有权限Toast.makeText(getActivity(), "读取联系人权限被拒绝", Toast.LENGTH_SHORT).show();}休息;默认:super.onRequestPermissionsResult(requestCode, permissions, grantResults);}}私有静态布尔hasPermission(上下文上下文,字符串权限){返回 ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;}}

在代码中,我调用了 Dialog Fragment 类的 requestPermissions 方法.所以我期待在同一个班上取得成绩.

感谢任何帮助.提前致谢!

<小时>

我在这里添加更多细节,以便对其他人更有帮助.以前我使用过 getChildFragmentManager() 来显示 DialogFragment.

ContactPickerDialog dialog = new ContactPickerDialog();dialog.show(getChildFragmentManager(), "联系人选择器");

但是由于 @CommonWare 要求我使用活动来显示 DialogFragment.我进行了以下更改并且可以正常工作.

ContactPickerDialog dialog = new ContactPickerDialog();dialog.show(getActivity().getSupportFragmentManager(), "联系人选择器");

解决方案

似乎有 Android 中的一个错误,其中嵌套片段不支持 onRequestPermissionsResult() 回调.对于 DialogFragment,解决方法似乎是让想要显示对话框的片段调用宿主活动上的方法,而活动显示 DialogFragment 本身.>

I have started to work on Android M runtime permission. Here I am facing the issue that if requestPermissions is called from Dialog Fragment class then onRequestPermissionsResult not getting called in the same Dialog fragment class. But if requestPermissions is called from Activity class or Fragment class then onRequestPermissionsResult method get called in the same class.

Here is my sample code:

public class ContactPickerDialog extends DialogFragment {
    private static final int READ_CONTACTS_REQUEST_CODE = 12;
    private Context mContext;

    private void loadContact() {
        if(hasPermission(mContext, Manifest.permission.READ_CONTACTS)){
            new ContactSyncTask().execute();
        } else {
            this.requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, READ_CONTACTS_REQUEST_CODE);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        Logger.d("TAG", "dialog onRequestPermissionsResult");
        switch (requestCode) {
            case READ_CONTACTS_REQUEST_CODE:
                // Check Permissions Granted or not
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    new ContactSyncTask().execute();
                } else {
                    // Permission Denied
                    Toast.makeText(getActivity(), "Read contact permission is denied", Toast.LENGTH_SHORT).show();
                }
            break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

    private static boolean hasPermission(Context context, String permission){
        return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;
    }

} 

Here in the code I am calling requestPermissions method of Dialog Fragment class. So I am expecting to get result in same class.

Any help is appreciated. Thanks in advance!


EDIT: Here I am adding more detail, so that it will be more helpful to others. Previously I have used getChildFragmentManager() to show the DialogFragment.

ContactPickerDialog dialog = new ContactPickerDialog();
dialog.show(getChildFragmentManager(), "Contact Picker");

But As @CommonWare asked me to use activity to show the DialogFragment. I have made following changes and it works.

ContactPickerDialog dialog = new ContactPickerDialog();
dialog.show(getActivity().getSupportFragmentManager(), "Contact Picker");

解决方案

There appears to be a bug in Android, where nested fragments do not support the onRequestPermissionsResult() callback. For a DialogFragment, a workaround appears to be to have the fragment wanting to show the dialog call a method on the hosting activity, and the activity shows the DialogFragment itself.

这篇关于对话框片段中未调用 onRequestPermissionsResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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