Not call onRequestPermissionsResult from fragment

2019-07-23 12:49发布

问题:

In my android project, I use this code to check & request runtime permissions:

public class AgentsFragmentMapTab extends RootFragment implements  ActivityCompat.OnRequestPermissionsResultCallback 
{    
     //..
     private boolean checkAndRequestPermissions() {
            int internet = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.INTERNET);
            int loc = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION);
            int loc2 = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION);
            List<String> listPermissionsNeeded = new ArrayList<>();
            if (internet != PackageManager.PERMISSION_GRANTED) {
                listPermissionsNeeded.add(Manifest.permission.INTERNET);
            }
            if (loc != PackageManager.PERMISSION_GRANTED) {
                listPermissionsNeeded.add(Manifest.permission.ACCESS_COARSE_LOCATION);
            }
            if (loc2 != PackageManager.PERMISSION_GRANTED) {
                listPermissionsNeeded.add(Manifest.permission.ACCESS_FINE_LOCATION);
            }
            if (BuildConfig.DEBUG)
                Log.d(TAG, "checkAndRequestPermissions_listPermissionsNeeded(" + listPermissionsNeeded.size() + ")");
            if (!listPermissionsNeeded.isEmpty()) {
                ActivityCompat.requestPermissions(getActivity(), listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), LOCATION_REQ_CODE);
                return false;
            }
            return true;
        }

        @Override
        public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

            if (requestCode == LOCATION_REQ_CODE) {

                } else {

                }
            }
        }

This code shows the permission dialog perfectly. But when I click on the Allow or Deny button, the method onRequestPermissionsResult() doesn't get invoked. Why isn't it getting invoked and how to achieve this?

回答1:

Firstly, do this in the parent activity.

...
Fragment theFragment = 
     getSupportFragmentManager().findFragmentById(R.id.your_fragment_name);
...         

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case LOCATION_REQ_CODE: {
            if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                if(theFragment instanceof AgentsFragmentMapTab) 
                    ((AgentsFragmentMapTab)theFragment).passPermissionResult(true);

            } else {
                if(theFragment instanceof AgentsFragmentMapTab) 
                    ((AgentsFragmentMapTab)theFragment).passPermissionResult(false);
            }
            return;
        }
    }
}

Then in your fragment, add a public method like so:

public void passPermissionResult(boolean allowed) {
    if(allowed) {
        // ALLOW clicked
    }
    else {
        // DENY clicked
    }
}


回答2:

Change

 ActivityCompat.requestPermissions(getActivity(), listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), LOCATION_REQ_CODE);

to:

requestPermissions(listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), REQUEST_CODE);

And also ContextCompat to ActivityCompat and getActivity() to getContext() like:

int loc2 = ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION, LOCATION_REQ_CODE);


回答3:

As mentioned in comments Override onRequestPermissionsResult in the parent activity

Make sure to call super.onRequestPermissionsResult from this activity's onRequestPermissionsResult

it will then send to your fragments which override onRequestPermissionsResult

Edited :

Here is what i did

i called requestPermissions(StringOfPermissions,YourRequestCode);

inside the fragment

And Here is the code i used to add the fragment inside the activity , pretty straight forward

getSupportFragmentManager()
            .beginTransaction()
            .add(R.id.container, BlankFragment.newInstance("",""))
            .disallowAddToBackStack()
            .commit();

I overridden onRequestPermissionsResult():

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

inside the activity and fragment

when i requested from the fragment it entered the activity and then it entered the fragment