Android M Permissions: onRequestPermissionsResult() not being called

188,101

Solution 1

This issue was actually being caused by NestedFragments. Basically most fragments we have extend a HostedFragment which in turn extends a CompatFragment. Having these nested fragments caused issues which eventually were solved by another developer on the project.

He was doing some low level stuff like bit switching to get this working so I'm not too sure of the actual final solution

Solution 2

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:

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

Solution 3

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)

Solution 4

I hope it works fine

For Activity :

 ActivityCompat.requestPermissions(this,permissionsList,REQUEST_CODE);

For Fragment :

 requestPermissions(permissionsList,REQUEST_CODE);

Solution 5

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.

Share:
188,101

Related videos on Youtube

Ayohaych
Author by

Ayohaych

I suck at programming

Updated on October 10, 2021

Comments

  • Ayohaych
    Ayohaych over 2 years

    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

  • Ayohaych
    Ayohaych over 8 years
    Thanks for the help. I did notice that there is a requestPermissions method if its in a fragment. I am in a fragment and I tried calling just requestPermissions, unfortunately it didn't work. I also tried what you mentioned and it also didn't work.
  • Anthony Bobenrieth
    Anthony Bobenrieth over 8 years
    It also occurs with noHistory activities: code.google.com/p/android-developer-preview/issues/…
  • Bala Vishnu
    Bala Vishnu over 8 years
    This worked for me in Android M. But just wondering if it would cause any issues in Pre-M devices?
  • MohammadReza
    MohammadReza over 8 years
    This is only working if the permission is asked inside android.app.fragment, and you have to import support-v13 library!
  • Tigran Sarkisian
    Tigran Sarkisian over 8 years
    "When in android.support.v4.app.Fragment, you should use simply requestPermissions " - thank you!
  • WindRider
    WindRider over 8 years
    What if I have a support fragment in AppCompatActivity?
  • Bonatti
    Bonatti over 8 years
    Also, note that if you target an API (such as 19 for example), you will still be under the old permission model system, and thus, do not receive the request permission related function calls
  • TouchBoarder
    TouchBoarder over 8 years
    see my answer if you have extended another activity that does not call super
  • Jutikorn
    Jutikorn over 8 years
    FragmentCompat.requestPermissions() doesn't work with android.support.v4.app.Fragment. Anyone know how to handle this.
  • JanPl
    JanPl over 8 years
    Stumbled upon this one. FragmentActivity.onRequestPermissionsResult(..) is where the call to fragment's onRequestPermissionsResult(..) is forwarded to.
  • Dean Wild
    Dean Wild over 8 years
    @AnthonyBobenrieth so what is the solution here for noHistory activites? We simply can't request permissions from them?
  • Murphy
    Murphy over 8 years
    This helped me solve my issue: I had to redirect the onRequestPermissionsResult from the parent fragment to the child (nested) fragment. Thanks for the tip!
  • kAmol
    kAmol about 8 years
    I'm facing same problem but I'm not able to get onActivityCreated() in my code. SDK 23.
  • almisoft
    almisoft about 8 years
  • kAmol
    kAmol about 8 years
    I'm not in fragment, I'm trying to do it on onCreate of an activity
  • almisoft
    almisoft about 8 years
    Hmm, or use onPause()?
  • fersarr
    fersarr about 8 years
    The link is broken @AnthonyBobenrieth. Is there any solution for no history?
  • Alex Fragotsis
    Alex Fragotsis about 8 years
    @Bonatti So if i try to request a permission in android 4.4.2 i won't get a result? Thus I cannot change the permissions , only see them?
  • Dusan Zivkovic
    Dusan Zivkovic about 8 years
    @AlexanderFragotsis You will get a result. Only difference is you wont be asked to grant permission, instead you will be automatically granted by the system.
  • Eric Schlenz
    Eric Schlenz about 8 years
    I would have rather not had to remove the noHistory attribute, but this was the solution that worked for me. Thanks!
  • guness
    guness about 8 years
    Wow, there something inconsistent here. I have two apps. One requires that and the other not.
  • Waclock
    Waclock almost 8 years
    Actually it does provide an answer to the question. Calling requestPermissions (instance method of fragment) instead of ActivityCompat.requestPermissions, the result method is triggered. Which is the main issue here. Thank you Don
  • Timmy Simons
    Timmy Simons almost 8 years
    @goodgamerguy make sure you dont use the request code in onRequestPermissionsResult of activity. if your activity also uses onRequestPermissionsResult callback you should provide default: super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  • Someone Somewhere
    Someone Somewhere almost 8 years
    When using Fragment.requestPermissions(), the parent Activity is actually receiving the onRequestPermissionsResult(). (This is with support library 23.3.0)
  • Someone Somewhere
    Someone Somewhere almost 8 years
    Actually this is the answer... FragmentCompat.requestPermissions(Fragment fragment, String[] permissions, int requestCode)
  • Udi Oshi
    Udi Oshi over 7 years
    Relates to android.support.v13.app.FragmentCompat
  • angryITguy
    angryITguy over 7 years
    plus one: for mentioning "callback is on the activity, not the fragment"
  • AnV
    AnV over 7 years
    @yavor87 What if I want to request permission inside a Service ?
  • Yohan Dahmani
    Yohan Dahmani almost 7 years
    I had the issue on Marshmallow but not on Nougat, thank you it worked for me
  • zulkarnain shah
    zulkarnain shah almost 7 years
    What about SDK 15 and lower ?
  • Sanjay Mangaroliya
    Sanjay Mangaroliya almost 7 years
    You don't need permission about SDK 15, only Marshmallow and above version need..
  • Mohammad Yahia
    Mohammad Yahia almost 7 years
    the super implementation calls the fragments implementation not hte framework
  • Mohammad Yahia
    Mohammad Yahia almost 7 years
    just stressing on the importance of calling super implementation from activity as here stackoverflow.com/a/35245235/3061221
  • Afshin
    Afshin almost 7 years
    Don't forget to add this to your activity: stackoverflow.com/a/33995827/3404007
  • Minas Mina
    Minas Mina over 6 years
    Requires API level 23... On earlier ones use FragmentCompat.requestPermissions().
  • CoolMind
    CoolMind over 6 years
    Add 65536 and 111 and you will get it. This is because of bit masks. See some topics.
  • Placeable
    Placeable over 6 years
    Please elaborate why.
  • Mahdi
    Mahdi about 5 years
    I have android.support.v4.app.Fragment and used requestPermission() but result just caame in Activity.
  • Big McLargeHuge
    Big McLargeHuge over 4 years
    Wow, Android documentation mentions nothing about this: developer.android.com/training/permissions/requesting.html
  • Big McLargeHuge
    Big McLargeHuge over 4 years
    FragmentCompat is deprecated as of API 27.1.0.
  • Ben.Slama.Jihed
    Ben.Slama.Jihed over 4 years
    my answer is deprecated please refer to : developer.android.com/training/permissions/requesting
  • Vivek Thummar
    Vivek Thummar about 3 years
    i have normal activity which extends AppCompatActivity,Should I still use super()?