3

Hello I had simple application where I am using the camera, so the user must grant permission for the camera.

So far my app had only Activity, but now I want to use Fragments. I got to fragments one is called FragmentScanCode (here I wanna use the camera ).

This is also the first fragment so I want to grant permission at the start of app.

There is my code from MainActivity, it works fine.

final int RequestCameraPermissionID = 1001;

onRequestPermissionsResult:

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case RequestCameraPermissionID: {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                        ActivityCompat.requestPermissions(MainActivity.this,
                                new String[]{Manifest.permission.CAMERA},
                                RequestCameraPermissionID);

                        //return;
                    }
                    try {
                        cameraSource.start(cameraView.getHolder());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
            }
            break;
        }
    }

I can't use this in my Fragment because ActivityCompat.

Is any possible solution to grant permission on fragment?

This is what I tried based on developer.google.com doc and it works

This is working code for me.

@Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                           int[] grantResults) {
        switch (requestCode){
            case RequestCameraPermissionID: {
                if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    if(getActivity().checkSelfPermission(Manifest.permission.CAMERA) !=
                    PackageManager.PERMISSION_GRANTED) {
                        requestPermissions(
                                new String[]{Manifest.permission.CAMERA},
                                RequestCameraPermissionID);
                        //cameraSource.start(cameraView.getHolder());
                        return;
                    }
                }
                captureCode();
            }
            break;
        }
    }

and also in my function captureCode()

try {
                        if (getActivity().checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {

                            requestPermissions(
                                    new String[]{Manifest.permission.CAMERA},
                                    RequestCameraPermissionID);
                            return;
                        }
                        cameraSource.start(cameraView.getHolder());

                    } catch (IOException e) {
                        e.printStackTrace();
                    }

1 Answers1

0

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. check this out

Qaiser Hussain
  • 170
  • 1
  • 11