Diffinitive rules for using Android's getBaseContext, getApplicationContext or using an Activity's "this" -


I did a lot of this question and found several different recommendations when using getBaseContext , getApplicationContext , or an activity of its own this .

Three rules that come many times and make a lot of sense -

  1. Long reference to a contextual activity getApplicationContext Should be used because it exists with your presence
  2. For those references whose life-cycle is bound to their activities, their own reference references ( it ) Should be used
  3. Carefully store the indicator carefully

    >

    I have seen so many examples, / p>

      intent = new intent (getBaseContext (), myClass.class); Contrary to   

    -

      intention intent = new intent (this, myClass.class);   

    Which is the correct and recommended method and why?

    GetBaseContext () is the method of reference text. And the referendum is, "Implementing the implementation of the context that passes all the calls in only one reference. The subclass can be modified to modify behavior without changing the original context." (According to Jawadox)

    Therefore it is used to hire calls in any other context.

Comments