-4

Is it a good practice to use application context instead of other context(Activity, Service and so on) if possible? Pros:

  1. Application context can help us avoid memory leak.
  2. Application context can help eliminate params in some circumstances
  3. Other things don't come to my head for now

Cons:

No clue for me after googling for a while and this is where this question from.

Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
Bin
  • 212
  • 2
  • 14
  • Refer the so question http://stackoverflow.com/questions/7298731/when-to-call-activity-context-or-application-context – Chandrakanth May 29 '15 at 11:04

1 Answers1

1

Is it a good practice to use application context instead of other context(Activity, Service and so on) if possible?

Not in general. Use Application when you know precisely why you are using Application.

Application context can help us avoid memory leak.

It can also cause memory leaks (creating a custom subclass of Application and storing things in there). It can also screw up your UI (by ignoring themes), screw up the back stack, etc.

Application context can help eliminate params in some circumstances

I have no idea what you mean by that, sorry.

This blog post by Dave Smith provides an excellent overview of different types of Context and their roles.

CommonsWare
  • 910,778
  • 176
  • 2,215
  • 2,253
  • Application context can help eliminate params in some circumstances: It happens when I want access to resouces such as strings or drawables in a helper class. I don't have to pass an Activity instance to it if using Application Singleton. – Bin Jun 01 '15 at 03:33