I have a fragment in which I have recyclerview and setting data in this recyclerview using recyclerview adapter.
Now, I am having a button in the adapter\'s list ite
in APIs below 23 you can create an interface in the activity then implement it in the child fragment and when you get permission request result in the activity pass it to the implemented interface in fragment. Its this simple :)
you can call the Fragment method below
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_CODE:
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 &&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission is granted. Continue the action or workflow
// in your app.
doActionBecauseNowYouCanBro();
} else {
// Explain to the user that the feature is unavailable because
// the features requires a permission that the user has denied.
// At the same time, respect the user's decision. Don't link to
// system settings in an effort to convince the user to change
// their decision.
showWhyRequestPermissionsAndDontBlockUserItsCalledManners();
}
return;
}
// Other 'case' lines to check for other
// permissions this app might request.
}
I requested location permission from a fragment and in the fragment, I needed to change this:
ActivityCompat.requestPermissions(getActivity(), new String[]{
Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, LOCATION_REQ_CODE);
to this:
requestPermissions(new String[]{
Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, LOCATION_REQ_CODE);
then the onRequestPermissionsResult was called in the fragment.
If you are working with Kotlin you should exectly specify that you are calling fragment method, not activity
fragment.requestPermissions(permissions, PERMISSIONS_CODE);
Android M Permissions: onRequestPermissionsResult() not being called
Request runtime permissions from v4.Fragment and have callback go to Fragment?
This accepted answer is not worked for me, so i found my own solution, explained below:
1.First i created a method, in fragment:
public static void MyOnRequestPermissionResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults){
if (requestCode == 1 && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "Permission: true");
} else {
Log.d(TAG, "permission: false");
}
}
2.And then called it from its underlying activity:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if(requestCode ==1){
SignupFragment.MyOnRequestPermissionResult(requestCode, permissions, grantResults);
}
}
And it is working ...
For the tragetSDK 28,The SDK check (>23) and requestpermissions from fragment would work. The ActivityCompat.requestPermissions is failing (if you set request code below 65536, the permission dialog would appear and if the user allows the permission, the permissions are provided, but no callback will happen. But if you set above 65536, ActivityCompat.requestPermissions will fail immediately. i don't know the reasoning behind this logic. may be a bug or intentional).
Working code:
if (Build.VERSION.SDK_INT >= 23) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, LOCATION_ACCESS_REQUEST);
}