0

Is there a callback function after executing permissionState.launchPermissionRequest() in the Accompanist Permissions library? I want to execute some code only after it finishes.

Raj Narayanan
  • 2,443
  • 4
  • 24
  • 43

2 Answers2

2

A more straight forward way is to use the rememberMultiplePermissionsState's or rememberPermissionState's onPermissionsResult or onPermissionResult callback function as documented in the API documentation and as shown below:

permissionsToRequest = listOf(                         
   Manifest.permission.READ_EXTERNAL_STORAGE,
   Manifest.permission.CAMERA,
)

val multiplePermissionsState = 
   rememberMultiplePermissionsState(permissions = permissionsToRequest) {            
      //will be called with whether or not the user granted the permissions
}
Raj Narayanan
  • 2,443
  • 4
  • 24
  • 43
1

I'm copying this example from their guide:

val cameraPermissionState = rememberPermissionState(
        android.Manifest.permission.CAMERA
    )

    when (cameraPermissionState.status) {
        // If the camera permission is granted, then show screen with the feature enabled
        PermissionStatus.Granted -> {
            Text("Camera permission Granted")
        }
        is PermissionStatus.Denied -> {
            Column {
                val textToShow = if (cameraPermissionState.status.shouldShowRationale) {
                    // If the user has denied the permission but the rationale can be shown,
                    // then gently explain why the app requires this permission
                    "The camera is important for this app. Please grant the permission."
                } else {
                    // If it's the first time the user lands on this feature, or the user
                    // doesn't want to be asked again for this permission, explain that the
                    // permission is required
                    "Camera permission required for this feature to be available. " +
                        "Please grant the permission"
                }
                Text(textToShow)
                Button(onClick = { cameraPermissionState.launchPermissionRequest() }) {
                    Text("Request permission")
                }
            }
        }
    }

The 2 callbacks are: PermissionStatus.Granted and PermissionStatus.Denied

user496854
  • 6,461
  • 10
  • 47
  • 84