0

I'm new at Android...I was looking at source code of popular open source android apps like android mail,calendar... and noticed that in each class,service,file... always passing the Context parameter and the class initialise it to his local instance of Context, it almost appears in every class, many times.

My question is why those classes are need to get it as a parameter of their constructor or method while they can get it alone with those methods?

1. getApplicationContext()
2.  getContext()
3. getBaseContext()

They can set it in the start and use it...why so many methods in one class getting it? do they get other Context then those that they can get using on of the 3 methods above? For example, I saw the Service is a subclass of a Context class, so he can basically use this keyword and not wait for it from the caller.

AlexKulr
  • 69
  • 11
  • you don't need to use all those. Activity and service extend from context u so you can directly use `this`. a follow up read http://stackoverflow.com/questions/7298731/when-to-call-activity-context-or-application-context. Regarding constructor: constructor of which class? – Raghunandan Jun 28 '14 at 15:54
  • each class that created have it's own constructor, and in the parameters one of them is the context for example `myClass (String firstName, String lastName, Context context) {this.context= context } ` – AlexKulr Jun 28 '14 at 15:59
  • is it a normal java class. if you need context there you need to pass it to the constructor. services activities no need for constructors. use `this` – Raghunandan Jun 28 '14 at 16:02
  • Ok so why should I pass it if I can get it in any class with `getApplicationContext` and save passing unnecessary parameters? – AlexKulr Jun 28 '14 at 16:06
  • read the link posted before. For some stuff you need Activity Context. – Raghunandan Jun 28 '14 at 17:59
  • @AlexKulr a normal class does not have a `getApplicationContext` method. If it needs a `Context` it has to be passed in. – Henry Jun 28 '14 at 20:08

0 Answers0