onRequestPermissionsResult not called on Fragments in Android

By | October 12, 2016

This is a common mistake that people make when coding for marshmallow.

When in AppCompatActivity, you should use ActivityCompat.requestPermissions;

When in android.support.v4.app.Fragment, you should use simply requestPermissions (this is an instance method of android.support.v4.app.Fragment)

If you call ActivityCompat.requestPermissions in a fragment, the onRequestPermissionsResult callback is called on the activity and not the fragment.

requestPermissions(permissions, PERMISSIONS_CODE);

If you are calling this code from a fragment it has it’s own requestPermissions method.

So basic concept is, If you are in an Activity, then call

ActivityCompat.requestPermissions(this,
                            new String[]{Manifest.permission.CAMERA},
                            MY_PERMISSIONS_REQUEST_CAMERA);

and if in a Fragment, just call

requestPermissions(new String[]{Manifest.permission.CAMERA},
                            MY_PERMISSIONS_REQUEST_CAMERA);

Check this post to know how to implement Camera Permissions in MarshMallow.

6 thoughts on “onRequestPermissionsResult not called on Fragments in Android

  1. Pingback: Simple Example on using CAMERA Access permission in versions greater than Android Marshmallow. – CoderzHeaven

  2. Paul Montoya

    Thanks, I was just thinking of exchanging my fragment for an activity, but it was not necessary

    Reply
  3. Pingback: [Solved] onRequestPermissionsResult not being called in fragment if defined in both fragment and activity

  4. Pingback: [Android] onRequestPermissionsResult not being called in fragment if defined in both fragment and activity - Pixorix

Leave a Reply

Your email address will not be published. Required fields are marked *