vmadalin / easypermissions-ktx

🔓 Kotlin version of the popular google/easypermissions wrapper library to simplify basic system permissions logic on Android M or higher.
https://firebaseopensource.com/projects/googlesamples/easypermissions/
Apache License 2.0
382 stars 38 forks source link

Infinite loop occurs when displaying SettingsDialog with onPermissionsDenied #11

Closed Nunocky closed 2 years ago

Nunocky commented 2 years ago

Basic Information

Device type: Emulator (Pixel4 API 29) OS version: Android 10 EasyPermissions version: 1.0.0

Question

Sorry for the series of questions.

I'm trying to create a function like "Open app settings when some permissions are permanently denied", but it doesn't work.

In the onPermissionsDenied callback, I evaluate somePermissionPermanentlyDenied() and open the SettingsDialog from there, but the Fragment from which the call is made is being called infinitely.

画面収録 2021-12-04 午前11 34 26

Is it wrong to try to open the SettingsDialog at this location? If so, how do I open the SettingsDialog correctly?

Code and logs

I have uploaded the source code to github. https://github.com/Nunocky/EasyPermissionsStudy

    override fun onResume() {
        super.onResume()

        checkPermissions()
    }

    //  -----------------------------------------------
    //  permissions issue
    //  -----------------------------------------------
    private val requiredPermissions = listOf(
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.CAMERA,
    )

    private fun checkPermissions() {
        if (!EasyPermissions.hasPermissions(
                requireContext(),
                *(requiredPermissions.toTypedArray())
            )
        ) {
            // Do not have permissions, request them now
            EasyPermissions.requestPermissions(
                host = this,
                rationale = "permissions required",
                requestCode = REQUEST_PERMISSIONS,
                perms = requiredPermissions.toTypedArray()
            )
        }
    }

    override fun onPermissionsDenied(requestCode: Int, perms: List<String>) {
        if (EasyPermissions.somePermissionPermanentlyDenied(this, requiredPermissions)) {
            toast("somePermissionPermanentlyDenied")

            SettingsDialog.Builder(requireContext())
                .build()
                .show()
       }
    }
jjerome00 commented 2 years ago

you need to remove your permission check from onResume()