1

My app uses "dangerous" permissions such as READ_CONTACTS, among others. All the examples I have seen to support this new model use classes derived from AppCompatActivity. Do I really have to change my activities that subclass Activity to now subclass AppCompatActivity? Is there an alternative way of doing this while still using activities? When I make that change my app crashes.

Floern
  • 33,559
  • 24
  • 104
  • 119
Phil O
  • 1,588
  • 4
  • 28
  • 52

1 Answers1

4

Do I really have to change my activities that subclass Activity to now subclass AppCompatActivity?

No.

Is there an alternative way of doing this while still using activities?

It's the same way. There is nothing in AppCompatActivity that has anything to do with runtime permissions. Whether you are inheriting from Activity, AppCompatActivity, or PhilodendronActivity, you:

  • Call checkSelfPermission() (on your Context or the static edition on ContextCompat) to see if you hold the permission

  • Call requestPermissions() (on your Activity or the static edition on ActivityCompat) to request the permissions from the user

  • Implement onRequestPermissionsResult() on your Activity to get the result of the requestPermissions() call

So, for example, none of these sample apps use AppCompatActivity:

  • An app just there to explore runtime permissions

  • The results of completing a hands-on tutorial to experiment with adding runtime permissions

  • Two apps that show requesting runtime permissions as part of setting up Play Services' fused location provider

  • An app that sets up runtime permissions for Maps V2 use

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thank you very much, I'll take a look shortly. Is it best to ask for all permissions at once, or one at a time, as dictated by user action? Read contacts is required at the beginning but write external storage may never be required if user doesn't take an action that requires it? – Phil O Apr 05 '16 at 12:22
  • @PhilO: Usually, you wait on requesting the permission until the user action, so you ask for as few permissions as possible. If the permission might not make sense to the user in the context of the user action, and you think that having some sort of "welcome" onboarding activity and asking for the permission up front might smooth things for the user, that's reasonable. – CommonsWare Apr 05 '16 at 12:27