简体   繁体   中英

onRequestPermissionsResult not called in fragment

In a DialogFragment I call requestPermissions, I tried those codes getActivity().requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},12);

or

requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},1);

I use this code to get the result

@Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        Log.i("permission", "Request Code: "+ requestCode);

if I put it in my HoneActivity it works fine, but I need onRequestPermissionsResult to be called in my fragment. If I put the code in the fragment where I called requestPermissions it don't work anymore. I also tried with s uper.onRequestPermissionsResult without success. In other topics I saw that there where a bug but I checked my version and I am using com.android.support:appcompat-v7:24.0.0 .

When you called this in your fragment

getActivity().requestPermissions(new String[{Manifest.permission.READ_EXTERNAL_STORAGE},12);

then

 @Override
 public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
   Log.i("permission", "Request Code: "+ requestCode);
 }

is getting called in your activity where you are adding your fragment.

So requestPermissions() in fragment and check onRequestPermissionsResult() in you parent activity.where you are adding you fragment.

The Reason is requestPermissions needs an activity as aurgument Check Full details here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM