I'm updating our app to use the new M runtime permissions system. It's all working except for onRequestPermissionsResult(). I need to check a permission on a button press, and if it's successful, send a text message. When I grant permission to do it, the dialog closes, but it doesn't trigger the Send Text until I press the button again.
I've debugged and set breakpoints in the onRequestPermissionsResult() method but it never goes into it.
This method gets called first:
private void askForPermission() {
String[] permissions = new String[]{Manifest.permission.SEND_SMS};
ActivityCompat.requestPermissions(getActivity(), permissions, PERMISSIONS_CODE);
}
And then my callback looks like this:
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PERMISSIONS_CODE) {
for (int i = 0; i < permissions.length; i++) {
String permission = permissions[i];
int grantResult = grantResults[i];
if (permission.equals(Manifest.permission.SEND_SMS)) {
if (grantResult == PackageManager.PERMISSION_GRANTED) {
onPPSButtonPress();
} else {
requestPermissions(new String[]{Manifest.permission.SEND_SMS}, PERMISSIONS_CODE);
}
}
}
}
}
Has anybody run into a similar issue? Appreciate any help with this. Thanks
I ran into the same issue and I just found the solution. When using the Support library, you have to use the correct method calls. For example:
If you call ActivityCompat.requestPermissions in a fragment, the onRequestPermissionsResult callback is called on the activity and not the fragment.
You can try this:
requestPermissions(permissions, PERMISSIONS_CODE);
If you are calling this code from a fragment it has it's own requestPermissions method. I believe the problem is that you are calling static method.
Pro Tip if you want the onRequestPermissionsResult()
in a fragment:
FragmentCompat.requestPermissions(Fragment fragment, String[] permissions, int requestCode)
I hope it works fine
For Activity :
ActivityCompat.requestPermissions(this,permissionsList,REQUEST_CODE);
For Fragment :
requestPermissions(permissionsList,REQUEST_CODE);
I encountered this problem too. If you want the activity that handles permissions not in the history/recents, then you will be tempted to change your AndroidManifest.xml
entry.
If you set the activity that you call requestPermissions
or AppCompatActivity.requestPermissions
with
android:noHistory="true"
android:excludeFromRecents="true"
in your AndroidManifest.xml
then onRequestPermissionsResult()
will not be called. This is true if your Activity is derived from Activity
or AppCompatActivity
.
This can be fixed by removing both flags from 'AndroidManifest.xml' and finishing your activity with finishAndRemoveTask()
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With