9

I'm calling ActivityCompat.requestPermissions in order to get permissions under android M, however, this requires an activity in the argument. This would be fine, except that I want to call it from a singleton, and the singleton can be used by any activity in the app.

ActivityCompat.requestPermissions(context, PERMISSIONS_LOCATION, REQUEST_LOCATION);

I want to avoid holding a reference to any activity within the singleton as that's a surefire recipe for a memory leak, and also I'd prefer that the singleton not hold an activity at all because it requires useless code in all the activities that call (every single one of them is going to have to include an extra argument in the getInstance() in order for the singleton to hold an activity - the singleton needs to get the activity from somewhere).

Now, I can technically get an activity and then set it to null straight after I request the permission, however that still leaves me with tons of useless activity arguments in every single activity where I make a call to the singleton. Is there a more elegant solution to this problem that I'm just not seeing?

Jon
  • 7,941
  • 9
  • 53
  • 105
  • you can actually cast context to activity when required if you are sure that the context variable holds reference to an activity. – Bhargav Aug 19 '15 at 13:54
  • @Bhargav I'm using getApplicationContext() as my context in order to avoid passing contexts from activities. I'm guessing I can't cast that to an activity? – Jon Aug 19 '15 at 13:56
  • If it requires activity as an object, Moving from one activity to another, its value should be updated. By doing that, I guess, It doesn't solve Singleton's purpose. – dhun Aug 19 '15 at 13:56
  • @dhun it just doesnt make a lot of sense to me. You request a permission for an *application* - not for a particular activity within an application. If you requested it then it's good for all the activities, so why are we being forced to tie the request to a specific activity within the app? – Jon Aug 19 '15 at 13:57
  • 1
    See this example for Runtime Request, [link] https://github.com/googlesamples/android-RuntimePermissions/blob/master/Application/src/main/java/com/example/android/system/runtimepermissions/MainActivity.java – dhun Aug 19 '15 at 14:05
  • @dhun - thanks, but interestingly enough that's the exact one I used to build my code in the first place. It doesn't really work for me because in the example he's calling it from an activity and I'm calling it from a singleton - which is exactly my problem – Jon Aug 19 '15 at 14:07
  • 1
    Right, but the target activity is a must to pass just to decide where to show the dialog asking for permissions. – dhun Aug 19 '15 at 14:09
  • Yes, this whole model is terribly cumbersome. If you have three different activities that all require the same permission, now you wind up with the same code for checking and requesting the permissions triplicated, being repeated in each of the three activities. What a waste. – eidylon May 30 '16 at 20:50

1 Answers1

10

The documentation on requestPermissions says that the activity parameter is the target activity where you want to show the pop up if you haven't included the permission in your manifest and for this purpose that method requires you to pass an activity and not the context, because upon finish the request permissions task it will then return a result to the calling activity(that is the activity passed as the parameter to the method). If you are so adamant about implementing this through your singleton I suggest you create a function that accepts the activity in the parameter and the callbacks too as you WILL need to handle the callbacks if the permissions were given or not

Bhargav
  • 8,118
  • 6
  • 40
  • 63
  • 2
    ok, that makes sense. I couldn't figure out why they were asking for an activity to begin with. I guess I'll just pass in activity references from every single calling class in the app :/ – Jon Aug 19 '15 at 14:10