In this article, we will see how to check permissions from the fragment in Android along with an example.

Before we see how to implement the code to check the permissions make sure the permissions are declared in the Manifest.xml file. Without declaring permissions in the manifest, your app may encounter runtime errors or unexpected behavior when attempting to access restricted resources or features.

Implement OnRequestPermissionsResultCallback

To handle permission requests and responses inside the fragment, it should implement the OnRequestPermissionsResultCallback. When the user grants or denies the requested permission, the onRequestPermissionsResult() callback will be executed.

Following is an example:

public class MyFragment extends Fragment implements ActivityCompat.OnRequestPermissionsResultCallback {
    // Fragment code here...
    
    // Implement onRequestPermissionsResult()
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        // Handle the permission request response
        // Check the requestCode to identify the specific permission request
        // Process the grantResults array to determine if the permissions were granted or denied
    }
}

requestPermissions()

We can use the requestPermissions() method available within the fragment to request permissions from the user. This method takes two parameters i.e. permission strings & request code.

Following is the example of requesting camera permission:

public class MyFragment extends Fragment implements ActivityCompat.OnRequestPermissionsResultCallback {
    // Fragment code here...
    
    private static final int PERMISSION_REQUEST_CODE = 100;
    
    private void requestCameraAndLocationPermissions() {
        String[] permissions = {Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION};
        requestPermissions(permissions, PERMISSION_REQUEST_CODE);
    }
    
    // Rest of the fragment code...
}

onRequestPermissionsResult()

The method onRequestPermissionsResult() will be called when the user responds to the permission request. In this callback, we can check whether the requested permission is granted or denied.

public class MyFragment extends Fragment implements ActivityCompat.OnRequestPermissionsResultCallback {
    
    private static final int PERMISSION_REQUEST_CODE = 100;
    
    // Implement onRequestPermissionsResult()
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == PERMISSION_REQUEST_CODE) {
            // Check if all permissions were granted
            boolean allPermissionsGranted = true;
            for (int result : grantResults) {
                if (result != PackageManager.PERMISSION_GRANTED)
                {
                    allPermissionsGranted = false;
                    break;
                }
            }
            
            if (allPermissionsGranted) {
               Println("Permission is granted");
            } else {
               Println("Permission is not accepted");
            }
        }
    }
  }

Categorized in:

Tagged in:

,