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