Hey guys I want to handle denied permission in jetpack compose. I found PermissionsRequired
method in which helpful in my case till now. I got a problem which I explain in detail later. First I'll explain what I did inside this function.
class BluetoothRequestActivity : BaseActivity() {
val permissions = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
listOf(
Manifest.permission.BLUETOOTH,
Manifest.permission.BLUETOOTH_ADMIN,
Manifest.permission.BLUETOOTH_SCAN,
Manifest.permission.BLUETOOTH_CONNECT,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
)
} else {
listOf(
Manifest.permission.BLUETOOTH,
Manifest.permission.BLUETOOTH_ADMIN,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
AbcTheme {
AppBarScaffold() {
val multiplePermissionsState = rememberMultiplePermissionsState(permissions = permissions)
PermissionsRequired(
multiplePermissionsState = multiplePermissionsState,
permissionsNotGrantedContent = {
setupBluetoothRequest(multiplePermissionsState)
},
permissionsNotAvailableContent = {
setupBluetoothRequest(openPermissionSetting = true)
}
) {}
}
}
}
}
@Composable
private fun setupBluetoothRequest(
multiplePermissionsState: MultiplePermissionsState? = null,
openPermissionSetting: Boolean = false
) {
BluetoothRequest(
multiplePermissionsState = multiplePermissionsState,
openPermissionSetting = openPermissionSetting,
onCancelClick = {
finish()
}
)
}
}
When first time user open the activity permissionsNotGrantedContent
will open it shows the Button to ask the permission. Now when user denied the permission and click not ask again options, I changed the view through permissionsNotAvailableContent
will tigger and Button functionality change to open System setting. I did this without any problem.
MAIN PROBLEM
All things happen then I closed my activity and Reopen the activity. My PermissionsRequired
always trigger permissionsNotGrantedContent
this function which is wrong. Right now according to flow user permanently disable the dialog so why this is trigger I want to handle this function setupBluetoothRequest(openPermissionSetting = true)
. Any idea how can I fix this problem? Thanks
UPDATE
I have 4 scenario when first user land on screen I want to show Text - Abc wants permission
and Button
. When user click on Button it asked runtime permission. It asked 2 options Allow and Denied. When user Denied 2 times I want to show in Text- you denied please manual open setting and give permission
. If user Allow then I want to show Text - Permission granted
on screen. I did all these 3 scenario without any problem. When user Denied the permission 2 times it shows Text- you denied please manual open setting and give permission
but when user close the screen and reopen screen I am getting Text - Abc wants permission
and Button
on screen which I don't want. So how can I solve this problem.
setContent {
AbcTheme {
AppBarScaffold() {
val multiplePermissionsState = rememberMultiplePermissionsState(permissions = permissions)
when {
multiplePermissionsState.shouldShowRationale -> {
setupBluetoothRequest(multiplePermissionsState)
}
multiplePermissionsState.permissionRequested -> {
setupBluetoothRequest(openPermissionSetting = true)
}
}
}
}
}
UPDATE 2
after updating from 0.23.1
to 0.25.1
I don't see the options of permissionRequested
anymore.
when {
multiplePermissionsState.allPermissionsGranted -> {
Text(text = "Permission Granted", color = Color.White)
}
multiplePermissionsState.shouldShowRationale ->{
Text(text = "shouldShowRationale", color = Color.White)
}
}
So how can I deal now?