如果在片段和活动中都定义了 onRequestPermissionsResult,则不会在片段中调用 [英] onRequestPermissionsResult not being called in fragment if defined in both fragment and activity

查看:20
本文介绍了如果在片段和活动中都定义了 onRequestPermissionsResult,则不会在片段中调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个片段,其中有 recyclerview 并使用 recyclerview 适配器在此 recyclerview 中设置数据.

I have a fragment in which I have recyclerview and setting data in this recyclerview using recyclerview adapter.

现在,我在适配器的列表项中有一个按钮,单击该按钮我需要检查 android 中的 READ_EXTERNAL_STORAGE 权限以获取 android 中的新权限模型.

Now, I am having a button in the adapter's list item clicking on which I need to check the READ_EXTERNAL_STORAGE permission in android for new permission model in android.

我在这个适配器的片段中创建了一个新函数来检查是否授予权限,如果尚未授予权限,则请求权限.

I have created a new function in this adapter's fragment to check if permission is granted or not and request for permission if not granted already.

我已将 MyFragment.this 作为参数传递到适配器中,并在适配器中单击按钮时调用片段的方法.

I have passed MyFragment.this as a parameter in the adapter and calling the fragment's method on the button click in the adapter.

我使用以下代码在片段中调用 requestPermission.

I have used the below code to call requestPermission in fragment.

if(ContextCompat.checkSelfPermission(mContext, Manifest.permission.READ_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED){
       requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                ConstantVariables.READ_EXTERNAL_STORAGE);
    }

我使用以下代码覆盖了片段中的 onRequestPermissionsResult 方法:

I have overridden the onRequestPermissionsResult method in fragment by using the below code:

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case ConstantVariables.READ_EXTERNAL_STORAGE:
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, proceed to the normal flow.
                startImageUploading();
            } else {}

但是它没有被调用,而是调用了这个 Activity 的 onRequestPermissionsResult 方法.

But it is not getting called, instead of this Activity's onRequestPermissionsResult method is getting called.

我也在片段的父活动中定义了相同的 onRequestPermissionsResult 方法,并且它正在被调用.

I have defined the same onRequestPermissionsResult method in fragment's parent activity also and it is getting called.

我无法删除活动的 onRequestPermissionsResult 方法,但想在我从片段请求权限时调用片段的 onRequestPermissionsResult 方法.我怎样才能做到这一点?我在这里做错了什么,如果有人在这里有想法,请帮助我.

I can not remove the activity's onRequestPermissionsResult method but want to call fragment's onRequestPermissionsResult method when I request permission from fragment. How can I do this? Am I doing something wrong here, please help me if anyone have idea here.

推荐答案

编辑答案以涵盖更广泛的问题

Edited answer to cover broader issues

我认为您混淆了片段和活动的方法.上个月我的项目有一个类似的问题.请检查您是否最终具备以下条件:

I think you are confusing the method for fragment and activity. Had a similar issue my project last month. Please check if you have finally the following:

  1. 在 AppCompatActivity 中使用方法 ActivityCompat.requestpermissions
  2. 在 v4 支持片段中,您应该使用 requestpermissions
  3. Catch 是,如果您在片段中调用 AppcompatActivity.requestpermissions,则回调将进入 Activity 而不是片段
  4. 确保从活动的 onRequestPermissionsResult 调用 super.onRequestPermissionsResult.

看看是否有帮助.

这篇关于如果在片段和活动中都定义了 onRequestPermissionsResult,则不会在片段中调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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