I\'m having a weird issue that is causing a conflict. I had to switch to native Fragments
to fix it, but there are bugs with that.
My original problem:
You can use this part of code
requestPermissions(new String[]{Manifest.permission.GET_ACCOUNTS}, PERMISSION_REQUEST_CODE);
I faced the same situation recently, when I needed to check for a permission inside the support fragment and get a callback there.
I was able to use ContextCompat
to checkSelfPermission
and then as @NasaGeek said called android.support.v4.app.Fragment
's requestPermissions
to request the permission and then got a call back to onRequestPermissionsResult
in v4 Fragment.
Hope this helps.
Thanks.
In my case I have requested the permission from the fragment and also need to get the response in fragment.
But my phone running on android 8.1
so I was need to add one more condition for check this
so eventually there is my solution
private void doOnWazeButtonClick()
{
int permissionStatus = PackageManager.PERMISSION_DENIED;
if (getContext() != null)
{
permissionStatus = ContextCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION);
}
if (permissionStatus == PackageManager.PERMISSION_GRANTED)
{
showContentWaze();
}
else
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
Objects.requireNonNull(getActivity()).requestPermissions(new String[] {Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE_PERMISSION_ACCESS_FINE_LOCATION);
}
else
{
requestPermissions(new String[] {Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE_PERMISSION_ACCESS_FINE_LOCATION);
}
}
}
Adding this to the parent activity works for me:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
List<Fragment> fragments = getSupportFragmentManager().getFragments();
if (fragments != null) {
for (Fragment fragment : fragments) {
fragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
Source: https://code.google.com/p/android/issues/detail?id=189121#c5
I don't know if it's recently fixed by google, but I can reach the expected result without doing any tricks.
The most important thing is to call super.onRequestPermissionsResult(requestCode, permissions, grantResults);
in the activity, so it will pass the result to fragment if it's requested from fragment.
So, what I do is:
1) in fragment, ask permission using v4 fragment.requestPermissions(permissions, requestCode)
2) in activity's onRequestPermissionsResult, must call
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
3) in fragment's onRequestPermissionsResult, write the code I want to handle the result.
v4.Fragment works well. I have an issue with nested fragment of v4.Fragment. Permission is asked, but the callback onRequestPermissionsResult() is never called in nested fragment!
Issue opened