Request runtime permissions from v4.Fragment and have callback go to Fragment?

前端 未结 10 904
我寻月下人不归
我寻月下人不归 2020-12-01 01:35

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:

相关标签:
10条回答
  • 2020-12-01 01:54

    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!

    0 讨论(0)
  • 2020-12-01 01:55

    Just use requestPermission("your permission/s in string array",your request code) simply no need to use Fragment.requestPermissons(String[],int );

    This method in your fragment calls requestPermissions of android.support.v4.app.Fragment class i.e

    public final void requestPermissions(@NonNull String[] permissions, int requestCode) {
            if (mHost == null) {
                throw new IllegalStateException("Fragment " + this + " not attached to Activity");
            }
            mHost.onRequestPermissionsFromFragment(this, permissions, requestCode);
        }
    
    0 讨论(0)
  • 2020-12-01 01:56

    At the moment the most stable solution is doing it by hand. I myself resolved it simply by notifying child fragments from the parent fragments.

    if (fragment.getParentFragment() != null) {
                                Fragment parentFragment = fragment.getParentFragment();
                                try {
                                    ChildFragmentPermissionRegistry registry = (ChildFragmentPermissionRegistry) parentFragment;
                                    registry.registerPermissionListener((ChildFragmentPermissionCallback) fragment);
                                    parentFragment.requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_REQUEST_CODE);
                                } catch (ClassCastException e) {
                                    Log.e(PermissionVerifier.class.getSimpleName(), e.getMessage());
                                }
                            } else {
                                fragment.requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_REQUEST_CODE);
                            }
    

    Where parent fragment implements interface ChildFragmentPermissionRegistry and registers child fragment,

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (callback != null) {
            callback.onRequestPermissionsResult(requestCode, permissions, grantResults);
            callback = null;
        }
    }
    

    and child fragments implements ChildFragmentPermissionCallback

    and interfaces are something like this:

    public interface ChildFragmentPermissionCallback {
        void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults);
    }
    
    public interface ChildFragmentPermissionRegistry {
        void registerPermissionListener(ChildFragmentPermissionCallback callback);
    }
    
    0 讨论(0)
  • 2020-12-01 02:01

    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.

    0 讨论(0)
提交回复
热议问题