Android M Permissions onRequestPermissionsResult() not being called
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
The onRequestPermissionsResult() method is a fundamental part of handling permissions in Android applications starting from Android 6.0 (Marshmallow), which introduced a new way of managing app permissions called the runtime permissions model. This model requires apps to request permissions from users while the app is running, rather than at installation time. However, developers often face an issue where the onRequestPermissionsResult() callback is not called, even after requesting permissions. Understanding why this happens and how to address it is crucial for building robust Android applications.
Understanding onRequestPermissionsResult()
onRequestPermissionsResult() is a callback method in an activity or fragment that's called when a user response to a permission request is received. The method has the following signature:
Here:
requestCodeis the integer request code originally supplied torequestPermissions(). This allows you to identify which permission request the user is responding to.permissionsis an array of the permissions requested.grantResultscontains the results of those requests eitherPackageManager.PERMISSION_GRANTEDorPackageManager.PERMISSION_DENIED.
Common Issues Why onRequestPermissionsResult() May Not Be Called
- Incorrect requestCode: If the
requestCodeused inrequestPermissions()doesn't match the one checked inonRequestPermissionsResult(), the callback might not be triggered correctly. - Overriding onActivityResult without calling super: If you override
onActivityResult()in your activity without callingsuper.onActivityResult(requestCode, resultCode, data), it may preventonRequestPermissionsResult()from being invoked. - Fragment not attached to Activity: In cases where permissions are requested from a fragment, if the fragment is not properly attached to its host activity when the results are dispatched, the
onRequestPermissionsResult()will not be called. - Calling requestPermissions() from the wrong context: If
requestPermissions()is called from an inappropriate context (for example, a non-activity context like application context), the system may not be able to handle the callback properly.
Debugging and Solutions
To troubleshoot and fix issues where onRequestPermissionsResult() is not being called, consider the following solutions:
- Check requestCode consistency: Ensure that the
requestCodepassed torequestPermissions()matches the one checked inonRequestPermissionsResult(). - Ensure proper use of onActivityResult: Always call
super.onActivityResult(requestCode, resultCode, data)in youronActivityResult()method to allow further processing in the activity's pipeline. - Manage fragment lifecycle: Confirm that the fragment is properly attached to its activity before requesting permissions and during the expected callback execution.
- Use appropriate context: Always use an activity context when calling
requestPermissions().
Summary Table
| Issue | Solution |
| Mismatched requestCode | Match the requestCode in both requestPermissions() and onRequestPermissionsResult() |
| Overridden onActivityResult | Always call super.onActivityResult(requestCode, resultCode, data) |
| Fragment lifecycle issue | Ensure the fragment is attached during permission requests and results |
| Incorrect context | Use activity context for requestPermissions() |
Additional Details
- Consider User Denials: Even if everything is implemented correctly, if the user denies the permission request, you must handle this gracefully. Consider adding logic to explain why the permission was needed or providing alternative ways to continue without granting the permission.
- Permission Grouping: Android groups certain types of permissions. If a user accepts or denies one permission in a group, this action may affect all permissions in the group. This could influence the behavior of subsequent requests and should be managed accordingly.
By understanding these aspects of the onRequestPermissionsResult() method and knowing what problems to look out for and how to address them, Android developers can ensure better management of app permissions, contributing to a smoother user experience and enhanced app functionality.

