简体   繁体   中英

Android - “Context of View” vs “Context of Activity”

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM