Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference and when to use getApplication(), getApplicationContext(), getBaseContext() and someClass.this

People also ask

What is the difference between getContext () getApplicationContext () getBaseContext () and this?

getApplicationContext() - Returns the context for all activities running in application. getBaseContext() - If you want to access Context from another context within application you can access. getContext() - Returns the context view only current running activity.

What is the difference between this context and getApplicationContext which one to use when?

This generally should only be used if you need a Context whose lifecycle is separate from the current context, that is tied to the lifetime of the process rather than the current component. Actually in general it is better to use getApplicationContext() since it will less likey lead to memory leaks.

What is the difference between getContext and getActivity?

getContext() - Returns the context view only current running activity. getActivity()- Return the Activity this fragment is currently associated with. getActivity() can be used in a Fragment for getting the parent Activity of the Fragment .

What is the difference between application context and activity context in Android?

Application Context: It is the application and we are present in Application. For example - MyApplication(which extends Application class). It is an instance of MyApplication only. Activity Context: It is the activity and we are present in Activity.


Toast and Intent, both requires reference to context. And getApplication, getApplicationContext, LoginActivity.this and getBaseContext, they all offer reference to the context.

Now the thing confuses is the declaration of different contexts and their specific-usage. To make things simple, you should count two types of context available in the Android framework.

  1. Application Context
  2. Activity Context

Application context is attached to the application's life-cycle and will always be same throughout the life of application. So if you are using Toast, you can use application context or even activity context (both) because a toast can be raised from anywhere with in your application and is not attached to a window.

Activity context is attached to the Activity's life-cycle and can be destroyed if the activity's onDestroy() is raised. If you want to launch a new activity, you must need to use activity's context in its Intent so that the new launching activity is connected to the current activity (in terms of activity stack). However, you may use application's context too to launch a new activity but then you need to set flag Intent.FLAG_ACTIVITY_NEW_TASK in intent to treat it as a new task.

Now referring to your cases:

LoginActivity.this though its referring to your own class which extends Activity class but the base class (Activity) also extends Context class, so it can be used to offer activity context.

getApplication() though its referring to Application object but the Application class extends Context class, so it can be used to offer application context.

getApplicationContext() offers application context.

getBaseContext() offers activity context.

Tips: Whenever you need to manipulate Views then go for Activity-Context, else Application-Context would be enough.


The answer by Waqas is very clear and complete, however I'd like to further clarify the difference between using this vs. getBaseContext(), or getApplication() vs. getApplicationContext(). Both Activity and Application extend not Context itself, but ContextWrapper, which is a

"Proxying implementation of Context that simply delegates all of its calls to another Context".

That "real" context is what you get by using getBaseContext().

So although this (for Activity) and getBaseContext() both give the activity context, they

  • (a) do not refer to the same object (this != getBaseContext()) and
  • (b) calling context through this is slightly less efficient, as the calls go through an extra level of indirection. I doubt it makes any practical difference, though.

The same logic applies to getApplication() vs. getApplicationContext().


LoginActivity.this 

the above line is an Activity which is obeveously a Context.. this is used when you create some AlertDialogs... At some places its compulsory that you use Activity Context...

getApplication()

Same here the make text method needs Context and Application itself implements Context

getApplicationContext()

this is most preferred way since this Context lives untill Application shuts down.

getBaseContext()

this Context is available to widgets and Views..

But All of them gives a Context object and nothing else..