Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about the context object

 
Kasun Wixkramanayake
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

in above code why we need to pass context object to text view.why don't we use above approach when we take widgets from xml layout.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can be sure that the code built into Android that is called to inflate XML layouts knows how to get the current Context object, and passes it into the TextView constructor, just like this code does.
 
Kasun Wixkramanayake
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the advantage of passing context object to the parameter.And what is meant by memory leaking in android application.is there any relation with between the memory leaking and context object. webpage.

[ UD: fixed URL ]
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what you mean by "advantage" - advantage over what? If the View needs the Context object, then it somehow needs to get hold of a reference to it.

There's nothing specific about the Context with regards to leaking memory. That can occur with just about any kind of object if you're not careful.
 
Kasun Wixkramanayake
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i mean why we pass context object as parameter.why we do that.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the View (TextView in this case) needs it. At any given moment there are many Views in existence; in order to visualize them correctly Android needs to know which context each belongs to.

Are there other design approaches where this isn't necessary, and the OS infers the context in some other way without the need for the developer to pass it in? Certainly. Why the Android team did not do it that way would be a question for them.
 
Kasun Wixkramanayake
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thnk you for the great explanation.now i know why we use context object.but i cant understand why we use context in database application.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't matter what the app does - there's always a Context. But then, I'm not sure what you mean by "database application", and what a Context may or may not have to do with it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic