Request runtime permissions from v4.Fragment and have callback go to Fragment?
Solution 1
This behavior seems to be present in the v4 Fragment support class requestPermissions in Fragment. The Activity/FragmentCompat implementations exist for people who wish to use the native classes with the extended functionality on API levels between 11 and 23.
Solution 2
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
Solution 3
If you need to get the permissionResult in fragment v4
make sure you use
Fragment.requestPermission(String[], int);
instead of
AppCompat.requestPermission(Activity, String[], int)
Check out this answer!
Solution 4
You can use this part of code
requestPermissions(new String[]{Manifest.permission.GET_ACCOUNTS}, PERMISSION_REQUEST_CODE);
Solution 5
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.
![RED_](https://i.stack.imgur.com/axgTy.png?s=256&g=1)
Comments
-
RED_ almost 2 years
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: I have a navigation drawer setup with
v4 Fragments.
To ask for permission in one of my Fragments I callActivityCompat.requestPermissions(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION, 1);
The prompt shows up just fine, but when I accept or deny the permission, nothing happens. The callbackonRequestPermissionsResult()
is never called. Instead it gets called in the Activity that my Fragments are attached to. Useless to me, I need the callback to work in the Fragment.With this in mind I was told that I need to use
FragmentCompat
, but that only works withnative Fragments (v13+)
, so I changed navigation drawer to work from native Fragments instead of the v4 support library Fragments. However, because I'm using AppCompatActivity, certain things do not work, likeaddToBackStack()
and going back to a previous fragment.Long story short, does anyone know how I can use the
v4.Fragment
and still call for permission in theFragment
and get the callback to be in theFragment
? I feel like this is a bug in Android that hasn't been addressed but I'm not 100%.Let me know if you need to see my code, it's just the standard methods that you need for runtime permissions, I would like to work with v4 Fragments though which doesn't work from my understanding.