1

I'm a beginner in android, and would appreciate any help.
Basically in androidx.fragment.app.Fragment after calling requestPermissions() , onRequestPermissionsResult() is never called.

I read on another question (onRequestPermissionsResult not being called in fragment if defined in both fragment and activity) that ActivityCompat.requestPermissions would result in activity's onRequestPermissionsResult method being called. and simple requestPermissions would call onRequestPermissionsResult in same fragment. I've tried both ways but every time activity's method is being called i think maybe because this is not a v4 support fragment but a androidx.fragment.app.Fragment.

I tried overriding onRequestPermissionsResult in activity and called super's method but still fragment's method is not being called.

private final String[] REQUIRED_PERMISSIONS = new String[]{"android.permission.CAMERA", "android.permission.WRITE_EXTERNAL_STORAGE"};
private int REQUEST_CODE_PERMISSIONS = 10;

@Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setupTimeOut();

        if (allPermissionsGranted()) {
            // Do stuff that requires permission
        } else {
            requestPermissions(REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == REQUEST_CODE_PERMISSIONS) {
            if (allPermissionsGranted()) {
                // Do stuff that requires permission
            } else {
                Toast.makeText(getViewContext(), "Permissions not granted", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private boolean allPermissionsGranted() {
        //check if req permissions have been granted
        for (String permission : REQUIRED_PERMISSIONS) {
            if (ContextCompat.checkSelfPermission(getViewContext(), permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

what i want is to get the permissions result callback in same fragment.
In case this is relevant, permissions are for camera and write external storage and fragment is a simple photo capture fragment.

Usman
  • 11
  • 1
  • 2
  • https://stackoverflow.com/questions/42275906/how-to-ask-runtime-permissions-for-camera/52201117#52201117 – Quick learner Oct 30 '19 at 09:14
  • @Quicklearner 'ActivityCompat.requestPermissions' callback method is supposed to be in base activity of fragment (which is what's used in linked answer). but i need to get call back in the same fragment. – Usman Oct 30 '19 at 10:19
  • you can use it in your child fragment anywhere you want to use – Quick learner Oct 30 '19 at 11:37
  • Sample code i shared is inside a child Fragment. But still `onRequestPermissionsResult` is not being called. Even though the Parent activity's `onRequestPermissionsResult` is ! maybe i'm missing something here ?!! – Usman Oct 30 '19 at 13:44

0 Answers0