Android M Permissions: onRequestPermissionsResult() not being called
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.
Related videos on Youtube
Comments
-
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 over 8 yearsThanks 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 over 8 yearsIt also occurs with noHistory activities: code.google.com/p/android-developer-preview/issues/…
-
Bala Vishnu over 8 yearsThis worked for me in Android M. But just wondering if it would cause any issues in Pre-M devices?
-
MohammadReza over 8 yearsThis is only working if the permission is asked inside android.app.fragment, and you have to import support-v13 library!
-
Tigran Sarkisian over 8 years"When in android.support.v4.app.Fragment, you should use simply requestPermissions " - thank you!
-
WindRider over 8 yearsWhat if I have a support fragment in AppCompatActivity?
-
Bonatti over 8 yearsAlso, 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 over 8 yearssee my answer if you have extended another activity that does not call super
-
Jutikorn over 8 yearsFragmentCompat.requestPermissions() doesn't work with android.support.v4.app.Fragment. Anyone know how to handle this.
-
JanPl over 8 yearsStumbled upon this one.
FragmentActivity.onRequestPermissionsResult(..)
is where the call to fragment'sonRequestPermissionsResult(..)
is forwarded to. -
Dean Wild over 8 years@AnthonyBobenrieth so what is the solution here for noHistory activites? We simply can't request permissions from them?
-
Murphy over 8 yearsThis 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 about 8 yearsI'm facing same problem but I'm not able to get
onActivityCreated()
in my code. SDK 23. -
almisoft about 8 years
-
kAmol about 8 yearsI'm not in fragment, I'm trying to do it on onCreate of an activity
-
almisoft about 8 yearsHmm, or use
onPause()
? -
fersarr about 8 yearsThe link is broken @AnthonyBobenrieth. Is there any solution for no history?
-
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 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 about 8 yearsI would have rather not had to remove the noHistory attribute, but this was the solution that worked for me. Thanks!
-
guness about 8 yearsWow, there something inconsistent here. I have two apps. One requires that and the other not.
-
Waclock almost 8 yearsActually 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 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 almost 8 yearsWhen using
Fragment.requestPermissions()
, the parent Activity is actually receiving theonRequestPermissionsResult()
. (This is with support library 23.3.0) -
Someone Somewhere almost 8 yearsActually this is the answer...
FragmentCompat.requestPermissions(Fragment fragment, String[] permissions, int requestCode)
-
Udi Oshi over 7 yearsRelates to android.support.v13.app.FragmentCompat
-
angryITguy over 7 yearsplus one: for mentioning "callback is on the activity, not the fragment"
-
AnV over 7 years@yavor87 What if I want to request permission inside a
Service
? -
Yohan Dahmani almost 7 yearsI had the issue on Marshmallow but not on Nougat, thank you it worked for me
-
zulkarnain shah almost 7 yearsWhat about SDK 15 and lower ?
-
Sanjay Mangaroliya almost 7 yearsYou don't need permission about SDK 15, only Marshmallow and above version need..
-
Mohammad Yahia almost 7 yearsthe super implementation calls the fragments implementation not hte framework
-
Mohammad Yahia almost 7 yearsjust stressing on the importance of calling super implementation from activity as here stackoverflow.com/a/35245235/3061221
-
Afshin almost 7 yearsDon't forget to add this to your activity: stackoverflow.com/a/33995827/3404007
-
Minas Mina over 6 yearsRequires API level 23... On earlier ones use
FragmentCompat.requestPermissions()
. -
CoolMind over 6 yearsAdd 65536 and 111 and you will get it. This is because of bit masks. See some topics.
-
Placeable over 6 yearsPlease elaborate why.
-
Mahdi about 5 yearsI have android.support.v4.app.Fragment and used requestPermission() but result just caame in Activity.
-
Big McLargeHuge over 4 yearsWow, Android documentation mentions nothing about this: developer.android.com/training/permissions/requesting.html
-
Big McLargeHuge over 4 yearsFragmentCompat is deprecated as of API 27.1.0.
-
Ben.Slama.Jihed over 4 yearsmy answer is deprecated please refer to : developer.android.com/training/permissions/requesting
-
Vivek Thummar about 3 yearsi have normal activity which extends
AppCompatActivity
,Should I still usesuper()
?