3

I have a nested Fragment inside an Activity where I am trying to request Permission for ACCESS_FINE_LOCATION & ACCESS_COARSE_LOCATION.

In Android.M it works fine callbacks in the same Fragment but in lollipop the callback comes but the request codes are empty.

This is my Fragment inside the MainActivity

private static final int MY_LOC_PERMISSIONS = 123;
private static GoogleMap mMap;
private LocationManager locationManager;

private void setUpMap() {
    if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            //Tried this from Fragment 
            requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},MY_LOC_PERMISSIONS);
            //Tried this from Activity -> calls to Activity.onRequestPermissionsResult
            ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_LOC_PERMISSIONS);
    }else{
        locationManager.removeUpdates(this);
    }
}

and the request Callback in the same Fragment

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case MY_LOC_PERMISSIONS: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                setUpMap();
            } else {
                // permission denied, boo! Disable the functionality that depends on this permission.
            }
            return;
        }
        // other 'case' lines to check for other permissions this app might request
    }
}

I also tried this from the activity callback but same issue endless loop since request codes are null when it is received in the Fragment from SO post Request runtime permissions from v4.Fragment and have callback go to Fragment?

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    List<Fragment> fragments = getSupportFragmentManager().getFragments();
    if (fragments != null) {
        for (Fragment fragment : fragments) {
            fragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}

I tried the same thing from an activity it works like a charm. But I need this in the fragment. Also I created a function where I can check for functions in my Activity but when I am trying to do the same in the Fragment it doesn't even let me compile, like in Activity

private boolean isRequestRequired() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
    return true;
}
if (checkSelfPermission(Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_GRANTED) {
    return true;
}
if (shouldShowRequestPermissionRationale(Manifest.permission.SEND_SMS)) {
    Snackbar.make(findViewById(R.id.fab), "Please allow required access", Snackbar.LENGTH_INDEFINITE)
            .setAction(android.R.string.ok, new View.OnClickListener() {
                @Override
                @TargetApi(Build.VERSION_CODES.M)
                public void onClick(View v) {
                    requestPermissions(new String[]{Manifest.permission.SEND_SMS}, MY_PERMISSIONS);
                }
            }).show();
} else {
    requestPermissions(new String[]{Manifest.permission.SEND_SMS}, MY_PERMISSIONS);
}
return false;
}

and in the activity I am checking like

if (!isRequestRequired()) {
    return;
}else{
  SmsManager sms = SmsManager.getDefault();
  sms.sendTextMessage(<NUMBER>,null,<TEXT>,null,null);
}

But the same thing in fragment in Android studio it errors out: "you have to check for request permission ..."

Community
  • 1
  • 1
joyBlanks
  • 6,419
  • 1
  • 22
  • 47
  • On Lollipop it shouldn't get into the case where you call `requestPermissions()`, you only need to prompt the user for permission at runtime on api-23 and up. – Daniel Nugent May 27 '16 at 00:53
  • Exactly but the target version is 23 min is 21 in Gradle and so It doesn't even let me save the Fragment file check the function `isRequestRequired()` above. I know I am missing something but not sure what. – joyBlanks May 27 '16 at 00:56
  • Did you solve the problem, I am having a very similar one? – BitByteDog Mar 29 '17 at 13:20
  • No. I couldn't find anything. I think you should be asking for permissions at the beginning for such special cases one or 2. others whenever user requests. – joyBlanks Apr 11 '17 at 21:04

0 Answers0