Pandiyan Mani
2 min readNov 10, 2021

Handling Multiple Permission using Jetpack Compose

Hi Friends todays we look in to how to handle multiple permission using Jetpack compose so in this example we use CAMERA and RECORD_AUDIO Permission

First we have to add google accompanist implementation in our app build gradle which make the permission process easy

implementation "com.google.accompanist:accompanist-permissions:0.21.1-beta"

Then we have too declare the listof() permission if you are going to use single permission then you can use rememberPermissionState()

val permissionsState = rememberMultiplePermissionsState(
permissions = listOf(
android.Manifest.permission.RECORD_AUDIO,
android.Manifest.permission.CAMERA
)
)

After that by using permission state we get permission in string format by using that we can compare camera or Record Audio inside we have several when statement like got permission display granted if its denied for the first time we show the use of the permission and if they deny again then we display message mentioning that it has to be enabled on app settings

permissionsState.permissions.forEach { permis ->
when(permis.permission)
{
android.Manifest.permission.CAMERA -> {
when {
permis.hasPermission ->
{
Text(text= "Camera Permission Granted")
}
permis.shouldShowRationale ->
{
Text(text= "Camera Permission for taking Photo")
}
!permis.hasPermission && !permis.shouldShowRationale ->
{
Text(text= "Camera Permission Denied.Go To App settings for enabling")
}
}
}
android.Manifest.permission.RECORD_AUDIO -> {
when {
permis.hasPermission ->
{
Text(text= "Record Permission Granted")
}
permis.shouldShowRationale ->
{
Text(text= "Record Permission for recordin Voice")
}
!permis.hasPermission && !permis.shouldShowRationale ->
{
Text(text= "Record Permission Denied.Go To App settings for enabling")
}
}
}
}
}

And i am launching that permission inside on start lifecyle

val lifecycleOwner = LocalLifecycleOwner.current
DisposableEffect(
key1 = lifecycleOwner,
effect = {
val observer = LifecycleEventObserver { _, event ->
if(event == Lifecycle.Event.ON_START) {
permissionsState.launchMultiplePermissionRequest()
}
}
lifecycleOwner.lifecycle.addObserver(observer)

onDispose {
lifecycleOwner.lifecycle.removeObserver(observer)
}
}
)

Final code

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.Text
import androidx.compose.runtime.DisposableEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import com.example.myapplication.ui.theme.MyApplicationTheme
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.permissions.rememberMultiplePermissionsState
import java.util.jar.Manifest

class MainActivity : ComponentActivity() {
@ExperimentalPermissionsApi
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MyApplicationTheme {
val permissionsState = rememberMultiplePermissionsState(
permissions = listOf(
android.Manifest.permission.RECORD_AUDIO,
android.Manifest.permission.CAMERA
)
)
val lifecycleOwner = LocalLifecycleOwner.current
DisposableEffect(
key1 = lifecycleOwner,
effect = {
val observer = LifecycleEventObserver { _, event ->
if(event == Lifecycle.Event.ON_START) {
permissionsState.launchMultiplePermissionRequest()
}
}
lifecycleOwner.lifecycle.addObserver(observer)

onDispose {
lifecycleOwner.lifecycle.removeObserver(observer)
}
}
)
Column(modifier = Modifier.fillMaxSize()
,horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center) {
permissionsState.permissions.forEach { permis ->
when(permis.permission)
{
android.Manifest.permission.CAMERA -> {
when {
permis.hasPermission ->
{
Text(text= "Camera Permission Granted")
}
permis.shouldShowRationale ->
{
Text(text= "Camera Permission for taking Photo")
}
!permis.hasPermission && !permis.shouldShowRationale ->
{
Text(text= "Camera Permission Denied.Go To App settings for enabling")
}
}
}
android.Manifest.permission.RECORD_AUDIO -> {
when {
permis.hasPermission ->
{
Text(text= "Record Permission Granted")
}
permis.shouldShowRationale ->
{
Text(text= "Record Permission for recordin Voice")
}
!permis.hasPermission && !permis.shouldShowRationale ->
{
Text(text= "Record Permission Denied.Go To App settings for enabling")
}
}
}
}
}
}

}
}
}
}

Responses (1)