Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between ActivityCompat and ContextCompat?

Tags:

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.

like image 282
Antuan Avatar asked Jul 06 '16 17:07

Antuan


People also ask

When should I use ContextCompat?

ContextCompat class is used when you would like to retrieve resources, such as drawable or color without bother about theme. It provide uniform interface to access resources and provides backward compatibility.

What is Activitycompat?

A helper for accessing features in Activity in a backwards compatible fashion. Construct this by using getActivityCompat(Activity) . This helper augments the included methods with data on instant apps.

How do I use ContextCompat checkSelfPermission?

To check if you have a permission, call the ContextCompat. checkSelfPermission() method. For example, this snippet shows how to check if the activity has permission to write to the calendar: int permissionCheck = ContextCompat.


1 Answers

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

like image 174
Marcin Orlowski Avatar answered Sep 23 '22 11:09

Marcin Orlowski