Abdallah Alaraby Abdallah Alaraby - 5 months ago 8
Android Question

When exactly to ask for Runtime Permission?

Since Runtime Permissions were introduced in Android Marshmallow, I've been meaning to apply the new way for requesting permissions and I've read and understood the dangerous permissions and how to check for them and request them.

The question is, If I have an

Activity
that contains a
Button
for opening Camera.
Do I check and request the permission when the Activity is opened? Or when the button is clicked?

Please explain why your suggested way is preferred.

Answer

Do I check and request the permission when the Activity is opened? Or when the button is clicked?

If the only purpose of the Activity is to take a picture when the button is clicked, I would request the permission when the activity is opened.

If the Activity has other roles, such that the take-a-picture button is only one feature out of many, I would ask for the permission when the user clicks the button.

In other words, once the user has requested to go down a path that will (almost) certainly need the permission, ask for the permission.