16

I just wanted to know how many ways to get the context, which method used in which situation. Which one better to use, and what is the main and key deference between them.

Puja Mishra
  • 245
  • 1
  • 3
  • 8
  • check this [SO](http://stackoverflow.com/questions/7298731/when-to-call-activity-context-or-application-context) – Srikanth Roopa Jul 17 '13 at 06:42
  • Hi puja http://stackoverflow.com/questions/4128589/difference-between-activity-context-and-application-context chech this. – Monty Jul 17 '13 at 06:52

2 Answers2

6

For Your better understands you should read android official blog. an also look at HackBod Answer.

There are some references URL which help you more about the context

  1. What exactly does using the Application Context mean?
  2. Difference between Activity Context and Application Context
  3. http://android-developers.blogspot.de/2009/01/avoiding-memory-leaks.html

Thanks

Community
  • 1
  • 1
Ashish Dwivedi
  • 8,048
  • 5
  • 58
  • 78
2

Context class represents the local environment of an App, It encapsulates all the services and resources available to the app. There is a base class ApplicationContext, and sub classes for components: Activity, Service etc.

Always prefer using ApplicationContext because it is global and doesn't cause serious issues if its leaked, that is: an unused reference of it stays and is not garbage collected.

Sometimes you have to use sub components like Activity or Service as context. Use this when creating Intents, or creating UI Elements, or showing a toast etc. That is: functions that are specifically bound to these component's identity, its UI or its display window.

S.D.
  • 29,290
  • 3
  • 79
  • 130