tbruyelle / RxPermissions

Android runtime permissions powered by RxJava2
Apache License 2.0
10.48k stars 1.31k forks source link

Twice receiving 'granted' value in onSubscribe #240

Open zezekalo opened 6 years ago

zezekalo commented 6 years ago

Here are my code:

`    override fun onAttach(context: Context) {
        super.onAttach(context)
        if (context is Activity) {
            rxPermissions = RxPermissions(context)
            fusedLocationClient = LocationServices.getFusedLocationProviderClient(context)
            settingsClient = LocationServices.getSettingsClient(context)
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        logger.w("onViewCreated: ")
        if (checkPermissions()) {
            initMap(savedInstanceState)
        } else {
            requestPermissions(savedInstanceState)
        }
    }

    private fun requestPermissions(bundle: Bundle?) {
        rxPermissions
                ?.request(android.Manifest.permission.ACCESS_FINE_LOCATION)
                ?.subscribe({ granted: Boolean ->
                    logger.w("requestPermissions: granted - $granted")
                    if (granted) {

                        startLocationUpdates()
                        initMap(bundle)

                    } else {
                        onError(ForceLeaveAppException(resources.getString(R.string.not_granted_permission_error_message)), null)
                    }
                }, { exception: Throwable? ->
                    logger.w("requestPermissions: exception - ${exception?.localizedMessage}")
                })
    }

    private fun checkPermissions(): Boolean {
        return rxPermissions!!.isGranted(android.Manifest.permission.ACCESS_FINE_LOCATION)
    }
`

The problem is that when I'm requesting permission ACCESS_FINE_LOCATION then I have got 2 (two) response in Subscriber and is shown 2 lines in logs MapFragment: requestPermissions: granted - true

Is that normal behavior?

tbruyelle commented 6 years ago

@zezekalo Please ensure onViewCreated is invoked only a single time.

If not that means you'll have to use an other method for requestPermissions.

https://github.com/tbruyelle/RxPermissions#important-read

zezekalo commented 6 years ago

Yes, I checked and onViewCreated is invoked one time. I noticed that if I request two permissions together like ?.request(android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION) so in that case I've got only one response in callback.

tbruyelle commented 6 years ago

in that case I've got only one response in callback

Use requestEach to have one response per permission (please check the README)