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

Mohammed Atif
  • 4,383
  • 7
  • 28
  • 57
Mubarak
  • 1,419
  • 15
  • 22
  • 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 – Mubarak Oct 03 '16 at 11:33

2 Answers2

-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.

Mohammed Atif
  • 4,383
  • 7
  • 28
  • 57
  • 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? – Lalit Poptani Oct 03 '16 at 11:05
  • 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. – Mubarak Oct 03 '16 at 11:33
  • @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+ – Mohammed Atif Oct 03 '16 at 11:51
  • @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 activity – Mohammed Atif Oct 03 '16 at 11:53
  • @MohammedAtif yes but asking same permission on 10 different screens I would consider it as duplication of code! – Lalit Poptani Oct 03 '16 at 11:58
  • 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 polymorphism – Mohammed Atif Oct 03 '16 at 12:01
-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.

  • 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? – Siddharth Lele Oct 03 '16 at 11:17
  • I explictly mentioned First activity(That means which opens first) , which checks the permission if given proceed, if not can close the application. – Manu Sharma Oct 03 '16 at 11:22
  • 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? – Siddharth Lele Oct 03 '16 at 11:31
  • 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. – Siddharth Lele Oct 03 '16 at 11:33
  • Agreed. In that case Base class is savier I guess. – Manu Sharma Oct 03 '16 at 11:37