The Application class is the base class of Android application. It is the first class that is initialized when the app is started. Therefore you can initialize here everything that needs to be ready when an activity or other application component is started.
When to override Application class?
Application class doesn’t have to be overridden all the time. I presume most of simple apps can use default Application class implementation without problems. Android documentation states that
There is normally no need to subclass Application. In most situations, static singletons can provide the same functionality in a more modular way. If your singleton needs a global context (for example to register broadcast receivers), include Context.getApplicationContext() as a Context argument when invoking your singleton’s getInstance() method.
Main reasons to override Application class are following:
• Getting access to the Application Context
• Global initialization
• Getting access to global methods and data
How to override Application class?
Before we go through use cases when overriding Application class is useful, let‘s look how we can do it. We need to create class and overide onCreate method. onCreate method is called – same as in activity – when Application is created.
public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); } }
Then we need to register this class in Manifest.xml inside application tag with fully qualified name:
<application android:name=".app.MyApplication" .. />
Getting access to the Application Context
We will not be able to get Application Context from context if we have no access to context:-) We have no trouble to ask for context inside Activity, Fragment, Service and some other places. But there are other places without context reference. Sometimes we can pass context inside the constructor or inject it using some dependency injection framework. But sometimes it is not possible or it requires passing context through too many layers. Getting application context from Application class through static method is easy:
public class MyApplication extends Application { private static Context context; public static Context getContext() { return context; } @Override public void onCreate() { super.onCreate(); context = getApplicationContext(); } }
Global initialization
A lot of application components need to be initialized before first activity or service is started. This includes things like analytics, crash reporting or persistence. OnCreate method initializing few components might look like this:
@Override public void onCreate() { super.onCreate(); JodaTimeAndroid.init(this); StethoConfig.initStetho(this); Fabric.with(this, new Crashlytics()); }
Getting access to global methods and data
We might also have some function that we can access from anywhere in the app. Application class seems like a good place to put it in. For example, we might need a reference to Google Analytics tracker:
public class MyApplication extends Application { private Tracker mAppTracker; public synchronized Tracker getTracker() { if (mAppTracker == null) { GoogleAnalytics analytics = GoogleAnalytics.getInstance(this) mAppTracker = analytics.newTracker(R.xml.ga_tracker); } return mAppTracker; } }