Android Permissions Handling Using Kotlin Higher-Order Function

Musab Alothman
The Startup
Published in
2 min readJan 31, 2021

--

We all know that handling Android Permission logic is full of boilerplate code, that’s why I searched for an idea to avoid this, I saw many ideas with coroutines and suspend functions and so on.

But some idea popped in my mind make it very simple to implement handling permissions with Higher-Order function, clean, simple and very easy to use.

Let’s get into it…

BaseActivity

This will be the BaseActivity we put our code here because we need to override onRequestPermissionsResult I believe there is no way else to do.

You may notice afterPermissionFunc this is the key to our trick.

SampleActivity

After creating out BaseActivity like above, let’s go to see how we can use it in SampleActivity with very easy, simple and clean why.

Below you can see we use checkAndRequestPermission that we’ve created in the BaseActivity class.

As you can see that was so easy, clean and simple.

checkSelfPermission missing

You may wonder why I didn’t use checkSelfPermission to see if the permission already granted, the thing is I though no need for that since onRequestPermissionsResult will retrieve the accepted once too, anyway if you have anything in you mind feel free to leave a comment.

This is it, thanks for reading and enjoy coding :)

--

--