What’s the problem?
I want to make this really cool app with data stored in databases, personal preferences saved for the user, cached images for quick loading and I have realized that I need a reference to the Context to access application specific resources and I have ended up passing my Activity to every class I have designed. Its ugly and potentially dangerous. What do I do?
The boring basics
Context acts as an interface to the global information about the application envirnoment. It is mainly used to load and access resources related to an application. For example, you would require the context to call methods to access the application related directories, application information, start/stop a service, launch an activity, etc. There are typically 2 types of contexts in an android application, Activity and Application, which are nothing but 2 classes that have been derived from the Context class and hence lets you accomplish all that is possible by Context. Mostly, an Activity instance is passed to functions that need access to the context. First let us decide on which context to pass in different circumstances before we figure out how to pass it effectively.
The Application Context is valid as long as the application is alive while the Activity Context is very specific to the Activity and should not be used outside its scope. The reason is that if you pass an Activity Context to a function that lives longer than the Activity, the garbage collector fails to collect it when the Activity is destroyed. In such circumstances, it is always recommended to pass the Application Context. But make sure that you do not pass the Application Context to a function which deals with UI, as it will cause runtime exceptions. Anything that deals with the GUI requires an Activity Context and will not work if you pass it an Application Context. So make sure that the function does not go out of the Activity’s scope.
The solution
So, now that we have the basics in place, lets tackle the problem. From the above discussion and the requirement, we have,
- In any non-GUI related context usage, we should always use the application context.
- We need a static class that can be used to get a reference to the application context from anywhere in the program.
This can be very easily achieved by subclassing the Application class and keeping a static method that returns the application context. You could also make it a singleton. The added benefit to subclassing is that it can be used to store the global application state. Here’s how to go about it.
Step 1:
[sourcecode language="java"]
public class CustomAppContext extends Application {
private static CustomAppContext context;
@Override
public void onCreate() {
super.onCreate();
context = this;
}
public static CustomAppContext getInstance(){
return context;
}
public static Context getContext(){
return context.getApplicationContext();
}
/*Add your other methods and global variables here*/
}
[/sourcecode]
Step 2:
In the AndroidManifest.xml, modify the
And that is how its done! You can now call the CustomAppContext from anywhere in your code and get the Application Context. No need to pass it to all your various functions requiring it.
Final Review
A quick wrap up of what you have learnt in the tutorial
- How to subclass an Application class and use it to get the Application Context instance anywhere in the application.
- Never pass the Activity Context to a function that outlives the Activity. It will lead to memory leaks.
- Never pass an Application Context to any class or function that deals with the GUI as it will produce runtime exceptions
Hi
thanks your post helped me