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

查看:105
本文介绍了如果在片段和活动中都定义了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. 捕获是指如果您在片段中调用AppcompatActivity.requestpermissions,则回调将进入活动状态而不是片段
  4. 确保从活动的onRequestPermissionsResult调用super.onRequestPermissionsResult.
  1. In AppCompatActivity use the method ActivityCompat.requestpermissions
  2. In v4 support fragment you should use requestpermissions
  3. Catch is if you call AppcompatActivity.requestpermissions in your fragment then callback will come to activity and not fragment
  4. Make sure to call super.onRequestPermissionsResult from the activity's onRequestPermissionsResult.

查看是否有帮助.

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

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