27

I'm trying to use the Android camera, for API 23 or above, it requires asking for permission at runtime. According to the documentation, I can accomplish that using, ActivityCompat or ContextCompat. I don't understand what are the difference between the two and their trade-offs.

Thank you for time.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Antuan
  • 501
  • 2
  • 6
  • 18

1 Answers1

33

I don't understand what are the difference between the two and their trade-offs

There's no trade-off really. Not sure why they wrote so - checkSelfPermission() is a method of ContextCompat and ActivityCompat is subclass (child) of ContextCompat so you can pass either one whenever object of ContextCompat class is required.

Inheritance hierarchy (docs):

enter image description here

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • 1
    Thank you Marcin! You answered my question. I didn't know that ActivityCompat was a direct child of ContextCompat. – Antuan Jul 07 '16 at 06:24
  • 1
    Glad it helped. But you can always check class hierarchy in documentation of each class, i.e. check [ActivityCompat docs](https://developer.android.com/reference/android/support/v4/app/ActivityCompat.html) and you see (on top) that it extends `ContextCompat` which extends `Object`. – Marcin Orlowski Jul 07 '16 at 10:29
  • 1
    I had the same question, and I think this answer could add a very brief note on why ContextCompat and ActivityCompat is used differently in all samples out there, as that is what is causing this confusion in the first place. – BjornW Mar 02 '19 at 13:02