Reputation: 165
This is a snippet of the code to request for permission to access fine location in device.
if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}
...
If instead I switched ContextCompat with ActivityCompat vice versa, would my code still function as desired? For instance,
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
ContextCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}
...
I know ContextCompat is a subclass of ActivityCompat, but what determines when to use ContextCompat or ActivityCompat as they both share the same methods?
Upvotes: 1
Views: 98
Reputation: 2405
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.
Upvotes: 1