SECRET OF CSS

2 Ways to Request Permissions in Jetpack Compose | by Igor Stevanovic | Jul, 2022


The best ways to request permissions in Jetpack Compose

Photo by Pathum Danthanarayana on Unsplash

Our first permission that we are gonna request will be Camera permission. To use the camera on our device, we need to request permission for it because it belongs to the “dangerous” ones right. First, we need to add this permission into the manifest file like this:

<uses-permission android:name=”android.permission.CAMERA” />

After that, we can go to our Composable in which we need to request this permission and create a launcher for activity results like this:

We create a launcher for activity results with which we are gonna launch an Android built-in dialog for permission requests. Activity returns a boolean which tells us If permission is granted or not. If it is granted we open the camera and if not we could show some info dialog or whatever you want. After that, we will add a button whose onClickcallback calls the checkAndRequestCameraPermission function. This function looks like this:

First, we check if the permission is already granted, if it is we open the camera and if it is not we request it via the launcher. Finally our main composable looks like this:

Now we are gonna request two permissions for location. We can request both of them at the same time and no need to request them one by one. Permissions are ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION. As we all know, the first step is to add them to our beloved manifest file:

<uses-permission android:name=”android.permission.ACCESS_COARSE_LOCATION” />
<uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION” />

Then we are gonna add a launcher for activity results and a button for requesting these permissions in the main Composable. Like this:

Now we have an array of permissions and a launcher activity that is requesting multiple permissions. This activity is gonna show us a built-in dialog that requests every permission that we need. It returns a map of String (permission name) as a key and Boolean (granted or not) as a value. Function checkAndRequesstLocationPermissions looks like this:

It will check every permission if it is granted or not. If all of them are granted we can use location, and if not we are gonna request those that we need.

That was cool right, now I am gonna show you something even better. That is accompanist permissions library, they always make our lives easier. We can use their permissionState and PermissionRequired composable to manipulate with the result of requests even better. First, we are gonna add the dependency in the app-level build.gradle file:

implementation “com.google.accompanist:accompanist-permissions:0.23.1”

Note: At the of writing this article, 0.23.1 was the newest stable version. Check if there is a newer version.

We are gonna request the same Camera permission, so don’t forget to delete an app from your phone or clear app storage. First, we need to create permissionState which will track the state of our permission:

val permissionState = rememberPermissionState(permission = Manifest.permission.CAMERA)

After that, we are gonna create CameraPermission composable and pass it the permission state as a parameter so it can handle permission. Inside this composable, we are using the accompanist’s PermissionRequired composable which takes in four parameters:
permissionState: the state of our permission that we previously created
permissionNotGrantedContent: content that should be shown after the permission is denied
permissionNotAvailableContent: content that should be shown if the permission is not available
content: Composable callback that will be called after the permission is granted
CameraPermission composable looks like this:

Note: we need to add ExperimentalPermissionsApi annotation because PermissionState is still experimental.

So basically in our main composable, it looks like this:

In the button onClick callback we just launch permission request with our permissionState, simple enough. Now we are gonna request multiple permissions for location. It is done similarly with a few modifications. Again, we first create the state of permissions:

Note: type of permissionsState is now MultiplePermissionState, where previously it was PermissionState.

Next is to create LocationPermissions composable which contains PermissionsRequired accompanist’s composable. It is similar to PermissionRequired, the difference is only in the first parameter which accepts now MultiplePermissionState as the parameter.

And that’s it. In our main composable we just launch multiple permission requests:

Cool! As you can see accompanist permissions library gives us more flexibility and makes our life easier. All of the source code you can find in my Github repo.

That would be all for this article, I hope you liked it!

Connect with me on:Github
Twitter





News Credit

%d bloggers like this: