1

To get the context of the activity we just type

this

To get context inside onClick(View view), we just have to use

view.getContext()

As, View Class use context extensively & View constructor always accepts context as param, I want to know if there is any difference between the context of the activity and the context of the view.

If there is any catch, what is it?

  • 1
    "I want to know if there is any difference between the context of the activity and the context of the view" -- There can be. For example, views can be created by a service, such as the infamous "Facebook chatheads" scenario. In that case, `getContext()` on the `View` will return the `Service` or perhaps some `ContextWrapper` wrapped around that `Service`. – CommonsWare Mar 02 '17 at 00:08

1 Answers1

1

In general, it probably won't matter which one you use in most situations.

The main use case is this:

"Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc."

https://developer.android.com/reference/android/content/Context.html

If you press "ctrl + b" on the object such as the View or Activity and everything it extends from, you will eventually get to the top. As long as whatever you're doing has what it needs then it should be fine. So for the typical things you will need you don't need to worry about it.

Michael Vescovo
  • 3,741
  • 4
  • 32
  • 45