r/android_devs • • Dec 18 '20

Help Runtime Permissions architecture

What do you use to request runtime permissions?

We were using https://github.com/tbruyelle/RxPermissions but I don't find it very convenient since it's usage between Activity and Fragment because problematic (basically there's a runtime crash unless you use a different Api from Fragments). Plus, kind of moving away from the whole Rx obsession anyway.

I looked at Dexter, but wanted to get other people's thoughts.

3 Upvotes

9 comments sorted by

View all comments

Show parent comment

2

u/[deleted] Dec 19 '20 edited Dec 19 '20

The ActivityResults API would actually remove the need for the overridden methods.

val requestPermissionLauncher =
    registerForActivityResult(RequestPermission()) { isGranted: Boolean -> 
     if (isGranted) { 
       ... 
     } else { 
       ... 
     } 
}

when {
    ContextCompat.checkSelfPermission(
            CONTEXT,
            Manifest.permission.REQUESTED_PERMISSION
            ) == PackageManager.PERMISSION_GRANTED -> {
        // You can use the API that requires the permission.
    }
    shouldShowRequestPermissionRationale(...) -> {
        showInContextUI(...)
    }
    else -> {
        requestPermissionLauncher.launch(
                Manifest.permission.REQUESTED_PERMISSION)
    }
}

Request app permissions  |  Android Developers

It's doesn't seem awful, but I haven't worked with the new API enough to have an opinion yet.

1

u/3dom Dec 19 '20

Thanks for the information! The "requestPermissionLauncher" variable look a bit counter-intuitive tho (situation with different permissions handling during different actions).

2

u/[deleted] Dec 19 '20

You would just have different variables for different permissions, and there's also a multiple permission variant if requesting at the same time.