2

If you are able to call view.getContext() to return the Activity's context under which the view instance is currently being rendered, why do some of the View family methods take a Context as a parameter?

Could this not be implicit, or are there occassions when getContext() is different from a Context passed to one of these methods?

Here is an example: http://developer.android.com/reference/android/widget/ViewAnimator.html#setInAnimation(android.content.Context, int)

Many thanks for clearing this up

Darius
  • 5,180
  • 5
  • 47
  • 62

2 Answers2

0

view.getContext() actually return's your Activity's context, not application's. That is the reason you need to provide such context when initializing a new View.

And there's a difference between these two contexts. An Activity's context is attached to that particular activity's life-cycle. However, the application's context is referring to application's life-cycle.

For more info, read this.

Community
  • 1
  • 1
waqaslam
  • 67,549
  • 16
  • 165
  • 178
  • Thanks, I've edited the question.. I think I used the term 'application' when I meant to imply the Activity context. Will this always be the same as .getContext(). Oh I've just seen your update.. so there are scenarios when there could be a difference? – Darius Oct 25 '12 at 13:27
  • view.getContext will always return your Activity's context under which it is visible. Therefore, in order to define a new view, you need to identify the context when initializing. – waqaslam Oct 25 '12 at 13:29
  • OK I get that, but if the Android Run-time held a currentContext value that was updated with each new Activity's Context, then couldn't the Views just rely on reading this value upon initialisation? – Darius Oct 25 '12 at 13:34
  • Activity's context (a.k.a Activity itself) changes as rapidly as you switch the activities, therefore your views must need to know under which context they are being initialized. So the only way I see is to provide it via view's constructor. When an activity dies (finished/killed), the views attached on it also dies. That is the reason it is not recommended to define anything related to Views as **static** (in order to avoid memory-leaks) because at some point you may not have the activity's context. – waqaslam Oct 25 '12 at 13:43
  • `getContext()` doesn't return the activity under which the view is *visible*: views don't have to be visible. You can create a `TextView` inside a `Service.onBind()`, set its text and measure its height. `Context` is an umbrella API to retrieve resources, accessing databases and so on. Technically, `View`s don't die. They don't have all of the `Activity` callbacks (create, resume, pause). They just expose two hooks (`onAttachedToWindow()` and `onDetachedFromWindow()`) likely intended to save memory when the widget doesn't have a surface for drawing (eg deleting any cached bitmap). – Raffaele Oct 25 '12 at 14:18
0

I recently read some API returning IBinder, like getWindowToken() and getApplicationWindowToken(). Quoting the latter:

Retrieve a unique token identifying the top-level "real" window of the window that this view is attached to. That is, this is like getWindowToken(), except if the window this view in is a panel window (attached to another containing window), then the token of the containing window is returned instead.

Maybe this IPC mechanism has something to do with the View API. Android designers are not stupid or zealous: if they require a Context to build a View, it means that a Context is all they need, so building a View must be possible with an application context, a service context and -of course!- an activity context, but an activity is not required. The Context is just an umbrella API to retrieve resources, accessing databases, building intents and the like.

This is not the answer to your question, but maybe can serve as a helper point. I'm looking for the answer myself. Hope your question gets the attention it deserves.

Raffaele
  • 20,627
  • 6
  • 47
  • 86
  • Thanks for your input, and the helpful resource. It's something that has always made me ponder since I first came to Android. I'm all for terser, simpler code so was just wondering if the Context param was unnecessary and could be implicitly determined. It seems a more complex issue than I first thought! – Darius Oct 25 '12 at 13:40
  • Views are not bound to Activities only. They can be added inside Fragments, Dialogs, Toasts, etc. And among these all, context is the common denominator. That is why it returns context instead of reference to an activity. – waqaslam Oct 25 '12 at 13:50
  • @Waqas it doesn't mean anything where a `View` can be added... You could write your `ViewRecyclerBin` and add views to it, and what? The point is that when you construct a `View` you must supply a `Context`: dialogs are not context, toasts are not context, and fragments are not contexts. `Context` isn't *common denominato* among anything, it's just an [umbrella API](http://developer.android.com/reference/android/content/Context.html) to get themes, resources, databases, intents and so on – Raffaele Oct 25 '12 at 14:03