top of page
Writer's pictureLearning Android

Difference Between getApplicationContext() and MainActivity.this

While learning Android Applications, you might have wondered why we sometimes use getApplicationContext() (e.g. for Intents) and sometimes we use MainActivity.this (such as in a Toast message). In this post, I will try to explain you the basic difference among these in simple terms.


The context tells us the background information of the application or activity. The getApplicationContext() is associated with the application and will not change during the entire lifetime of the application;


Whereas, the MainActivity.this (or simply this, since it extends activity class) is an Activity context which is related to the particular activity, it is referring to. This type of context is destroyed as soon as the activity is destroyed.


The getApplicationContext() can be used for both the cases. So when we are creating for example a Toast message, we can choose either MainActivity.this or getApplicationContext(), since the Toast messages can be displayed from anywhere in our application.


64 views0 comments

Kommentare


bottom of page