How to centralize the run time permission when we have multiple activity
Asked Answered
C

2

6

Sorry for my bad english.

I have multiple Activity in my app, I need to centralize the runtime app permission.

Do I need to use BaseActivity?

If yes, please suggest me how to handle it in case of many Activity in app.

If no, please suggest better approach to handle it.

I want to reduce the code redundancy. Not interested to write same code again and again with every Activity.

I am looking for negative scenario also where user denied the permission and i have to show rational instead of keep asking to allow permission. and based on that i have to provide message or i have to update UI

Calicut answered 3/10, 2016 at 10:52 Comment(1)
I am looking for negative scenario also where user denied the permission and i have to show rational instead of keep asking to allow permission. and based on that i have to provide message or i have to update UICalicut
S
-1

Run time permission were introduced to have control over the dangerous resources

So ideally you must be checking for the permission every time you use a resource. This code must not be eliminated.

Then when it comes to request a permission, it is a single line code. I don't think you must be having a trouble with this.

Now comes the tricky part. Handling the permissions that are given. You can definitely have a base class (not recommended) but ideally different permission is used for different purpose.

For example location permission:
In same Activity say Location.java, i might need location permission for getting the address of the person using LatLon values, and in the same activity I am using location permission for live tracking the user.

Now to handle different implementations for same permission, you must have unique permissions codes based on the purpose, but not based on the resource you are accessing permission for. Handling all these things in a base class can be tricky. So ideally you must handle permission in the activity where it belongs. It will keep your code safe and prevent any mix ups with other codes.

Always advisable to read the official docs.

There might be cases where you might get confused with redundancy of the codes and multiple implementations. For that, Android is handling most of the code in its end, as a programmer, least expected is to check for permissions and perform appropriate operations wherever dangerous resources are used.

Snowcap answered 3/10, 2016 at 11:1 Comment(6)
So, you mean we should prefer duplication of code? If I have 10 Activities where I need Location the I should write same code in all 10 Activities?Skellum
I am looking for negative scenario also where user denied the permission and i have to show rational instead of keep asking to allow permission. and based on that i have to provide message or i have to update UI.Calicut
@LalitPoptani, there is a difference between duplicate code and multiple implementations for similar code. Asking permissions is the least expected from programmers of API 23+Snowcap
@Mubarak, for negative case you can have a class with static method where you can pass context, messaage and other details which can be used for rationale. Its common sense that no matter in which activity permission is denied, it will result in similar code. but for positive case you need to write permission code in each activitySnowcap
@MohammedAtif yes but asking same permission on 10 different screens I would consider it as duplication of code!Skellum
It is not asking same permission on 10 different screens. It is checking whether the user has given permission for that particular resource or not. As user can always go to settings and remove the permission for that app. And as far as negative response is considered, you can have a single code for all. But for a positive response, you must consider the concept of run time polymorphismSnowcap
E
-2

If you have 10 activities and all of them need location permission.Then I guess it is very crucial permission for your application.

For crucial permission, if not allowed by user you can close your application, hence do it on first activity itself. Like facebook does for various permission. It might be bad experience for user but It is necessary permission for your application.

Just write it once in the first activity and stop application if not permitted by user. A lot of flagship application does that.

Enthymeme answered 3/10, 2016 at 11:14 Comment(5)
Actually. No! If a user denies a permission after having granted it earlier, and opens an activity which neither checks nor prompts the user for accepting the permission, what happens then?Churr
I explictly mentioned First activity(That means which opens first) , which checks the permission if given proceed, if not can close the application.Enthymeme
Okay. So as an example, lets use the Facebook app requiring the Location permission. You click on the icon and on the first screen you accept the permission. Later, you revoke it. Then, you share something from to Facebook from the third party app which simply opens a prompt and not the app itself. There, you click on the "location pin" icon which requires the permission you had revoked, What then?Churr
My intention is not to argue. My point simply is, asking for permission/s only in one activity is not a proper suggestion or implementation.Churr
Agreed. In that case Base class is savier I guess.Enthymeme

© 2022 - 2024 — McMap. All rights reserved.