2

Currently, I'm learning Android development and while learning I have come across several things like passing 'this' context, ActivityCompat, and ContextCompat. What is ActivityCompat and why, how, and where we should use it? I have also tried to read the documentation for it but as I am new to the android I'm not able to understand the way it was written in the documentation.

Also what is context parameter in android and what is it work and how to use it. As i often see that whenever context is asked in a method 'this' keyword is passed to it. Could you please explain it in easy to understand language?

ADITYA RAJ
  • 123
  • 1
  • 2
  • 8
  • `I'm not able to understand the way it was written in the documentation.` there's a lot of documentation you wouldn't understand by reading it unfortunately, what do you need to do ? why are you interested in this class ? – a_local_nobody Jun 08 '22 at 14:08
  • @a_local_nobody I'm interest in these class because I'm learning android development and while watching the tutorial, I came across the class ActivityCompat and ContextCompat as it was frequently used in that code. But in the tutorial, there was nothing mentioned about these classes and it use. Why this class is used and what is it even? ActivityCompat and ContextCompat was used while written code for permission request from the user. – ADITYA RAJ Jun 08 '22 at 14:12
  • i understand you're learning, that's why i'm asking where you found it or where you saw it, often times you don't need a complete understanding of a class, rather just what a specific method is doing, sometimes finding documentation for a class is harder than finding out what a method does – a_local_nobody Jun 08 '22 at 14:23

1 Answers1

2

ActivityCompat is a Java class with only static members (similar to object in Kotlin). This means you never instantiate it. It only provides helper functions.

ActivityCompat specifically provides alternatives to some of the functions in a regular Activity that can be used when the functionality you want to use is different across different versions of Android.

For example, Android 9 (SDK 28) apparently slightly modified the details of what happens when Activity.recreate() is called. If your minSdkVersion is set to lower than 28, you might want to ensure the behavior is the same even on devices that are running older versions of Android. To do this, instead of calling recreate() in your Activity, you would call ActivityCompat.recreate(this).

There are various other "Compat" classes like this in the libraries, such as ViewCompat, WindowCompat, and WindowInsetsCompat.

Don't confuse ActivityCompat with AppCompatActivity, which is the class you normally will subclass to create your own Activities.

Tenfour04
  • 83,111
  • 11
  • 94
  • 154
  • Do you mean these are just the advanced versions of existing classes that deal with compatibility as per the SDK version? – ADITYA RAJ Jun 08 '22 at 14:20
  • 1
    No. ActivityCompat is not an Activity. It's a container of helper functions for working *with* Activities. If the Jetpack libraries were written in Kotlin, it wouldn't exist at all--they would have written these as extension functions. But in Java, functions *have* to go in a class, so they stuck them in something named ActivityCompat. Don't confuse ActivityCompat with AppCompatActivity, which is the class you normally will subclass to create your own Activities. Very confusing, I know. – Tenfour04 Jun 08 '22 at 14:24
  • Okay, I got it, Could you please explain to me what is context and why I often see that the 'this' keyword is passed when a method has a context parameter? As far as I know, 'this' is used to refer to the current instance of the class. Could you please explain it in a easy to understand language with some real examples? – ADITYA RAJ Jun 09 '22 at 15:15
  • It's explained in detail here: https://stackoverflow.com/questions/3572463/what-is-context-on-android Since an Activitiy is a subclass of Context, you can pass `this` as a context parameter if the code you're writing is inside your Activity class. That won't work in a Fragment because a Fragment is not a subclass of Context. It is a confusing topic, yes, and many would consider the implementation of Context/Activity/Fragment to be a pretty big design blunder. – Tenfour04 Jun 09 '22 at 16:40