Android | Android Context | Different Types Of Context | Application Context | Activity Context | getContext()
Android | Context
What is context in android?
Context is an interface to global information about an application environment.
This is an abstract class whose implementation is provided by the Android system.
Context is the context of the current state of the application/object. It lets newly-created objects understand what has been going on.
It allows access to application resources, classes as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.
It allows our application components to interact with the outside android environment like local files, databases, class loaders associated to the environment, services including system-level services, and more.
There are many different types of context in android. Wrong use of context can easily lead to memory leak in an android application.
So, let's give a look to type of context, how to use & when to use.
Application Context - getApplicationContext()
It is an instance which is singleton. This context is tied to the lifecycle of an application.
Where to use?
The application context can be used where we need a context whose lifecycle is separate from the current context or when we are passing a context beyond the scope of an activity.
We use getApplicationContext() if we need a context for something who may live longer other than likely context (like activity context)
Where not to use?
Application Context is not a complete Context, supporting everything that Activity does. Various things that we will try to do with this Context will fail, mostly related to the GUI.
How to get Application Context?
In Activity we can get application context by simply using following method.
getApplicationContext()
In case of fragment, we can get application context by using following code snippet:
getActivity().getApplicationContext();
getContext().getApplicationContext();
Note: In Fragments, when we call getActivity(), we get hosted Activity instance which is a Context as well.
But when we call getContext(), we get a Context which might not be an Activity (As Fragments now can be hosted by anyone implementing FragmentHostCallback interface).
Follow the following steps to get the application context in non-activity classes.
Step 1. Create java class that extends Application class as shown below:
package in.blogspot.technophilegeek.mycontextdemo; import android.app.Application; import android.content.Context; public class MyApplication extends Application { private static Context sContext; @Override public void onCreate() { super.onCreate(); sContext = getApplicationContext(); } public static Context getContext() { return sContext; } }
Step 3. Now we can get application context by simply writng MyApplication.getContext();
Example: Pass Application context if we are creating a singleton object for our application and that object needs context.
Passing activity context in the above situation will lead to memory leak as it will keep the reference to the activity and activity will not be garbage collected even if the respective activity is finished and is of no use.
Activity Context:
Activity context is available in an activity. This context is tied to the lifecycle of an activity. The activity context should be used when we are passing the context in the scope of an activity or we need the context whose lifecycle is attached to the current context.
Where to use?
If we have to create an object whose lifecycle is attached to an activity, we can go with the activity context.
getContext() in Content provider:
This context is the application context and can be used similar to the application context. This can be accessed via getContext() method.
Something in Air:
Let’s take a closer look at the line of code that displays the toast:
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
The first parameter of the Toast.makeText() method is the context in which we want the toast to appear. When we create a toast in an activity, we use this keyword to pass it the instance of the current activity.
This doesn’t work in a service, because the service context doesn’t have access to the screen. Whenever we need a context in a service in situations like this, we must use getApplicationContext() instead.
This gives us the context for whatever app happens to be in the foreground when the code run. It means that the service will be able to make a toast appear, even if we’ve switched to a different app.
nice article.
ReplyDelete