Home > Software engineering >  Is there a callback function for launchPermission in the Accompanist library
Is there a callback function for launchPermission in the Accompanist library

Time:08-18

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

CodePudding user response:

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

  • Related