1

I don't know when to use someActivityClass.this and when to use getApplicationContext() to set as a Context ! What is the difference ? When do I have to use the first option and when do I have to use the second option ?

pheromix
  • 18,213
  • 29
  • 88
  • 158
  • 3
    http://possiblemobile.com/2013/06/context/ – laalto Mar 30 '15 at 07:33
  • possible duplicate of [Difference between getContext() , getApplicationContext() , getBaseContext() and "this"](http://stackoverflow.com/questions/10641144/difference-between-getcontext-getapplicationcontext-getbasecontext-and) – Anggrayudi H Mar 30 '15 at 09:23

1 Answers1

2

Its very small but very important difference in android application code.

android.content.Context

Context is an abstract class whose allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.

1. getApplicationContext(): Application context is attached to the application's life-cycle.

2. LoginActivity.this : Activity context is live with the Activity's life-cycle and can be destroyed if the activity's onDestroy.

3. getBaseContext() : This Context is available to widgets and Views.

For more detail :

http://developer.android.com/reference/android/content/Context.html

When to call activity context OR application context?

Difference between getContext() , getApplicationContext() , getBaseContext() and "this"

Getting the Application Context

Community
  • 1
  • 1
Yogendra
  • 4,817
  • 1
  • 28
  • 21