- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference between getContext(), getApplicationContext(), getBaseContext() and this in android?
What is Context in Android?
A Context gives us information about the current state of an application. It allows us to interact with Android Components. It allows us to access files and other resources such as pictures, Activities, Fragments and Services. In Android getContext(), getApplicationContext(), getBaseContext(), this are the methods to retrieve different types of Context objects. The Context Class provides access to several resources and services that are needed to build an Android application including −
Resources − The resources such as layouts, images and strings which are stored in resource objects are accessed through the Context.
System Services − It provides access to system services such as Power Manger, Notification Manager and Location Manager.
Preference − The Preference is used to store and retrieve data in key value pair which can be accessed through Context.
Asset Manager − It is used to access raw asset files that are packaged with the application.
Databases − it provides access to the database used by the application.
What are the two types of Context?
There are two types of context which are as follows −
Activity Context
Application Context
In Android, both “Activity” and “Application” are subclasses of the “Context”class, which provides access to application-specific resources and classes.
The primary difference between both of these contexts is that “Activity” context is tied to the lifecycle of an “Activity” , while “Application” context is tied to the lifecycle of the entire application.
What is getContext() in Android?
getContext() method is generally used with Activities and Fragments to retrieve the context of the view. Context represents the current state of an application or activity that provides access to various resources such as strings, images and many more. getContext() can be called from any View which includes Button view, Text view or Image View.
Here is how you can use getContext() in Android application
public class CustomView extends View { public CustomView(Context context) { super(context); } public void displayToastMessage() { // Get the context associated with this view Context context = getContext(); // Use the context to access resources from string.xml file. String message = context.getString(R.string.my_message); // Using the context to display the toast message. Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); } }
What is getApplicationContext() in Android?
getApplicationContext() is a method provided by Context class. Context represents the current state of an object. getApplicationContext() can be called from any Activity, Services, BroadcastReceiver or ContentProvider to retrieve global application Context. This method can be safely used across multiple components of the application.
Here is how you can use getApplicationContext() in Android application
public class MyApp extends Application { @Override public void onCreate() { super.onCreate(); // Get the global application context Context context = getApplicationContext(); // Use the context to access strings.xml file. String message = context.getString(R.string.my_message); //Using the context to display the toast message on below line. Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); } }
What is getBaseContext() in Android?
getBaseContext() is a method provided by ContextWrapper class which returns the base context of the current context. getBaseContext() can be called from any Activity, Services, BroadcastReceiver or ContentProvider to retrieve global application Context. The method returns a Context object that can be used to access resources or services in an application.
Here is how you can use getBaseContext() in Android application
The Context returned by the getBaseContext() is used as a base Context For an Activity or Services. You can safely use it to access that resource or services that are joined to Activities and Services.
public class MyApp extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get the base context of this activity on below line. Context context = getBaseContext(); // Use the context to get data from strnig.xml file String message = context.getString(R.string.my_message); // using the context to display toast message on below line. Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); } }
What is this in Android?
this is a keyword in Android which is used to get the context of the current class in which we are present. Instead of getting context we can simply use this keyword which will provide the context of that current class.
Here is how you can use this in Android application
In this example instead of getting context stored in the variable we are simply passing as this to display a toast message.
public class MyApp extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Toast.makeText(this, message, Toast.LENGTH_SHORT).show(); } }
Difference between getContext(), getApplicationContext(), getBaseContext() and this
getApplicationContext() |
getBaseContext() |
getContext() |
this |
|
---|---|---|---|---|
Definition |
getApplicationContext() returns the global context of the application. |
getBaseContext() returns the context of the entire application. |
getContext() returns the context of the current activity. |
this is used to refer to the current activity. |
Parent |
Application |
Application |
Activity |
Activity |
Accessible outside the services |
Yes |
Yes |
No |
No |
Accessible outside content providers |
Yes |
Yes |
No |
No |
Accessible outside activity |
Yes |
Yes |
No |
No |
Conclusion
In summary, getContext() returns the Context of the current View or Fragment, getApplicationContext() returns the Context of the application, and getBaseContext() returns the Context of the ContextWrapper and this returns the context of the current class. It's important to choose the correct method depending on the context in which you are working, as using the wrong method can lead to memory leaks or other issues.