If location is disabled, I want to ask the user to enable it. For that, a dialog is displayed and user can open settings screen of the device and enable location. But after that, my app has dead. Clicking back button on the device goes just to settings and eventually home page. The only way is for me to open my app and start capturing details from zero again, which is bad experience.
Is there a way to listen to the selection of the user and if he grants permission, I continue with the data collection flow? Here is the portion that asks for user location enabling.
@Composable
private fun RequestEnableLocationDialog(onRequestPermissionResult: (Boolean) -> Unit) {
val context = LocalContext.current
val openSettingsLauncher =
rememberLauncherForActivityResult(
contract = ActivityResultContracts.StartActivityForResult(),
) {
onRequestPermissionResult(
locationEnabled(context),
)
}
AlertDialog(
onDismissRequest = { onRequestPermissionResult(false) },
title = { Text(text = "Enable Location") },
text = { Text(text = "You need to enable location...") },
confirmButton = {
TextButton(
onClick = {
openSettingsLauncher.launch(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS))
},
) {
Text(text = "Open Settings")
}
},
dismissButton = {
TextButton(
onClick = { onRequestPermissionResult(false) },
) {
Text(text = "Cancel")
}
},
)
}
private fun locationEnabled(context: Context): Boolean {
val locationManager =
context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
return LocationManagerCompat.isLocationEnabled(locationManager)
}