lundi 5 février 2018

Android dynamic permission request design pattern

I am quite new in android programming. I would like to ask about startActivityForResult() and ActivityCompat.requestPermissions() function and their design. I understand that result of those functions is handled by another Activity functions (onActivityResult() and onRequestPermissionsResult() respectively). But I don't understand why is it designed this way.

Especially with ActivityCompat.requestPermissions(). Why do I have to control if I have permission (ContextCompat.checkSelfPermission()), if I don't then ask for it (ActivityCompat.requestPermissions()). And then handle in completely different function if I got this permission or not?

I would expect somethink like:

askPermission(Context context, String permission, Runnable permissionGranted, Runnable permissionDenied)

which would call permissionGranted if I already have permission or if I got it from user. With this function I would have to care just if I have permission or I don't have it.

Now I have to distunguish if I have permission and then do synchronous task or I don't have it and then do "asynchronous" task in onRequestPermissionsResult() where I very often do the same, as I do if I already have permission.

My question is: Is there some reason, why are permissions designed this way? Is there some funtion as I wrote above to allow me just say what to do if I have and what to do if i don't have permission (in functional way)? Or is there some desing pattern to easy handle permissions and starting activities for result?

Thanks for your time and some explanation if you know why is this design good.

Aucun commentaire:

Enregistrer un commentaire