我已经开始对版本的Android M运行许可工作。在这里,我所面临的问题,如果 requestPermissions 是称为对话片段类,那么 onRequestPermissionsResult 没有得到所谓的在同一个对话片段类。但是,如果 requestPermissions 是活动名为类或片段类那么 onRequestPermissionsResult 方法被调用在同一个班级。
下面是我的示例code:
公共类ContactPickerDialog扩展DialogFragment { 私有静态最终诠释READ_CONTACTS_REQUEST_ code = 12; 私人语境mContext; 私人无效loadContact(){ 如果(hasPermission(mContext,Manifest.permission.READ_CONTACTS)){ 新ContactSyncTask()执行(); }其他{ this.requestPermissions(新的String [] {} Manifest.permission.READ_CONTACTS,READ_CONTACTS_REQUEST_ code); } } @覆盖 公共无效onRequestPermissionsResult(INT申请code,的String []的权限,INT [] grantResults){ Logger.d(TAG,对话onRequestPermissionsResult); 开关(要求code){ 案例READ_CONTACTS_REQUEST_ code: //检查授予的权限或不 如果(grantResults [0] == PackageManager.PERMISSION_GRANTED){ 新ContactSyncTask()执行(); }其他{ // 没有权限 Toast.makeText(getActivity(),读取联系人权限被拒绝,Toast.LENGTH_SHORT).show(); } 打破; 默认: super.onRequestPermissionsResult(要求code,权限,grantResults); } } 私有静态布尔hasPermission(上下文的背景下,弦乐许可){ 返回ContextCompat.checkSelfPermission(背景下,许可)== PackageManager.PERMISSION_GRANTED; }}在这里,在code我打电话的 requestPermissions 方法对话框片段类。所以,我期待得到导致同一类。
任何帮助是AP preciated。在此先感谢!
编辑:在这里,我增加更多的细节,所以,这将是对别人更有用。previously我用getChildFragmentManager()显示DialogFragment。
ContactPickerDialog对话框=新ContactPickerDialog();dialog.show(getChildFragmentManager(),联系人选择器);但作为 @CommonWare 问我用活动来显示DialogFragment。我做了以下变化和它的作品。
ContactPickerDialog对话框=新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不会被调用对话框片段
发布评论