0

Hi I am new to android and jetpack compose. I have a class 'PermissionHandler' to handle the permission request for android app. It is extended by 'AppCompatActivity' class. I am able to request permission. But the overridden func 'onRequestPermissionsResult' is not called when the options are selected.

Below is the class>>

class PermissionHandler(context: Activity): AppCompatActivity(){

    private val context: Activity = context
    private lateinit var permissionClass: String
    private lateinit var alertBody: String
    private lateinit var toastMessage: String

    companion object {
        const val PERMISSION_REQ_CODE = 100
    }

    private fun checkPermission(): Boolean{
        return (ContextCompat.checkSelfPermission(context, permissionClass) == PackageManager.PERMISSION_GRANTED)
    }

    private fun checkPermissionRationale(): Boolean{
        return ActivityCompat.shouldShowRequestPermissionRationale(context, permissionClass)
    }

    private fun requestPermission(){
        ActivityCompat.requestPermissions(context, arrayOf(permissionClass), PERMISSION_REQ_CODE )
    }

    @Composable
    fun ShowPermissionExplanation(){
        val alertInput = remember { mutableStateOf(false) }
        if(!alertInput.value) {
            AlertExplanation(
                title = "Permission Needed",
                body = alertBody
            ) { value -> alertInput.value = value }
        } else {
            requestPermission()
        }
    }

    @Composable
    fun CheckAndReqPermission(
        permissionName: String,
        permissionValue: (Boolean) -> Unit
    ){
        permissionClass = permissionName
        alertBody = "The app requires ${permissionClass.split('.')[2]} permission for particular feature to work as expected"
        toastMessage = "Please allow ${permissionClass.split('.')[2]} permission to the APP."

        if(checkPermission()){
            permissionValue(true)
        } else if (checkPermissionRationale()){
            ShowPermissionExplanation()
        }else{
            requestPermission()
            Toast.makeText(context, toastMessage, Toast.LENGTH_SHORT).show()
            permissionValue(false)
        }
    }

    
    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        Log.d("permission result", grantResults.toString())
        Log.d("permission result", "checking Log if the callback is happedning or not")

    }
}

The class object is simply called from a compossable func>>

@Composable
fun CheckBluetoothPermission(context: Activity) {

    val permissionValue = remember{ mutableStateOf(false) }
    val permissionHandler = PermissionHandler(context = context)
    permissionHandler.CheckAndReqPermission(AppPermission.ACCESS_FINE_LOCATION.permission) { value ->
        permissionValue.value = value
    }

I want the callback func 'onRequestPermissionsResult' to be triggered when the user selects any of the below option>>enter image description here

Karthik
  • 11
  • 2

1 Answers1

1

You should use rememberLauncherForActivityResult and its callback for same functionality to 'onRequestPermissionsResult'

I think this answer is able to help you: How request permissions with Jetpack Compose?