Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
420 views
in Technique[技术] by (71.8m points)

android - onRequestPermissionsResult not being called in fragment if defined in both fragment and activity

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

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.

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

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

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

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

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

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.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

See if it helps .


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...