135

I'm new to android and I'm trying to understand the difference between getApplication(), getApplicationContext(), getBaseContext(), getContext() and someClass.this and especially when to use the these methods in the following code lines:

When I launch a toast what is the difference between these and in which cases to I use them?

Toast.makeText(LoginActivity.this, "LogIn successful", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplication(), "LogIn successful", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "LogIn successful", Toast.LENGTH_SHORT).show();
Toast.makeText(getBaseContext(), "LogIn successful", Toast.LENGTH_SHORT).show();

same with intents:

Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
Intent intent = new Intent(MenuPagina., LoginActivity.class);
Intent intent = new Intent(getBaseContext(), LoginActivity.class);
Intent intent = new Intent(getApplication(), LoginActivity.class);
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Pheonix7
  • 2,131
  • 5
  • 21
  • 38
  • Possible duplicate of http://stackoverflow.com/q/1026973/741249 or http://stackoverflow.com/q/6854265/741249 – THelper Apr 27 '12 at 08:21
  • they only handle the context and baseContext. but i would like to understand all their usages in the 2 contexes. thanks – Pheonix7 Apr 27 '12 at 08:24

4 Answers4

237

Toast and Intent, both require reference to context. Methods like getApplication, getApplicationContext, LoginActivity.this and getBaseContext offer reference to the context.

Now the confusing thing 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 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.

coroutineDispatcher
  • 7,718
  • 6
  • 30
  • 58
waqaslam
  • 67,549
  • 16
  • 165
  • 178
  • 11
    so if i get it right, someClass.this, getBaseContext and getContext() are activity contexes while getApplicationContext() and getApplication() are application contexes? – Pheonix7 Apr 27 '12 at 08:41
  • context is just information about an environment right? – committedandroider Nov 20 '14 at 04:03
  • Yes, its like the sandbox that hosts the application – waqaslam Nov 20 '14 at 04:06
  • @committedandroider because activity context contains extra info for the chosen theme, so when views are created in code then the correct theme will be applied automatically. For more info, you may read [this](http://www.doubleencore.com/2013/06/context/) – waqaslam Nov 30 '14 at 06:51
  • @waqaslam referring to android sdk Classes, `Context` -> `ContextWrapper` -> 1.`Application` & 2.`ContextThemeWrapper` -> `Activity`; and `getBaseContext()` is method of `ContextWrapper`, so `getBaseContext() offers activity context.` as well as `Application Context` too...isn't that? – Bhuro Sep 06 '16 at 15:18
  • lol, what is about the `WindowManager$BadTokenException` that is raised once you tried to call a toast from a finished activity? This literally means that the toast is attached or am I wrong? Or is there a difference between saying " – hannojg May 07 '18 at 21:11
  • @HannoGödecke that means you are trying to show a toast using the context of activity whose window is not alive anymore (or finishing). I would suggest you to use application's context for things like toasts. – waqaslam May 08 '18 at 06:15
  • @waqaslam okay, but isn`t that in contradiction to saying "...or even activity context because a toast can be raised from anywhere within your application and is not attached to a window"? Because when I am using an activity instance it gets more or less attached to a window? – hannojg May 08 '18 at 08:30
  • That statement is referred to the use of toast as asked by OP in his example code. But when the activity context is destroyed then there's no point in using its context otherwise exception will be thrown in logs. You can show toasts from activity-context as long as onDestroy is not called (or the activity is not killed), otherwise stick to application-context for this purpose since it's single instance throughout the application. – waqaslam May 08 '18 at 09:05
31

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().

fawaad
  • 341
  • 6
  • 12
AlexR
  • 625
  • 7
  • 10
  • 2
    As far as `getBaseContext()` is concerned, Google's Android Developers suggested this: "Don't use getBaseContext(), just use the Context you have." So, in effect, they suggest you use `this` for Activities. – ChuongPham Dec 19 '12 at 04:53
  • this is the most clear explanation of `getBaseContext` I found on the Internet. Thanks!! – q126y Jul 24 '16 at 17:56
6
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..

ngesh
  • 13,398
  • 4
  • 44
  • 60
  • so in a toast message or when creating an intent, they all have the same effect eventhough using getApplicationContext() is the most correct? – Pheonix7 Apr 27 '12 at 08:37
  • 1
    @Hassan Samii: For Toast, you can use `getApplicationContext()` for all situation, but it's preferable that you use `this` when making Toast in an Activity. – ChuongPham Dec 19 '12 at 04:56
0

Class.this used if your class extends Activity getapplication() used refer application and application extends application context getbasecontext()refer your activity context context refer to your activity life cycle context applicationcontext refer to your app life cycle