The Android Application class is a base class whose single instance is created before any activity, service or receiver in your process, and it lives until the process dies. You subclass it to run code once at startup and to hold process-wide setup such as dependency injection, logging, crash reporting and notification channels. Declare it with the android:name attribute on the <application> element in the manifest.
Use it for initialisation, not as a dumping ground for shared state. This guide shows how to create one, what belongs there, and what to avoid.
Extend Application, override onCreate(), and register the class in the manifest.
Inside any activity you can read it through application (typed as Application) or cast it. Every Context also exposes applicationContext.
Application has fewer callbacks than an activity.
| Callback | When it runs |
|---|---|
attachBaseContext() | First, before onCreate(); the context is not usable yet for most calls |
onCreate() | Once, when the process starts, before components are created |
onTrimMemory(level) | The system asks you to release memory |
onConfigurationChanged() | A configuration change affects the whole app |
onLowMemory() | Legacy low memory signal for old devices |
There is no onTerminate() on a real device. That callback is only called on emulators, so never rely on it to save data. Persist data as the user works, or in an activity or service lifecycle callback.
Important detail: the Application is created for every process start, including when the system starts your process only to deliver a broadcast, run a WorkManager job or answer a content provider. A heavy onCreate() therefore slows every one of those cases.
Good uses are small and global:
@HiltAndroidApp).ProcessLifecycleOwner observers.ProcessLifecycleOwner comes from androidx.lifecycle:lifecycle-process and gives you a single foreground or background signal for the whole app.
With Hilt, the entire class is a single annotation:
onCreate(): it delays the first screen. Defer it, run it on a background thread, or use App Startup (androidx.startup), which initialises libraries lazily and in order.Application, so those fields come back empty, and it is hard to test.Prefer dependency injection or a repository to a static singleton on the application class, so tests can replace dependencies.
The androidx.startup library replaces many content-provider-based initialisers with one provider you control. Write an Initializer:
Declare it in the manifest under the InitializationProvider with a <meta-data> entry. Libraries use the same mechanism, so you can disable or reorder them. For work that does not need to happen before the first screen, call it lazily when first used.
When Android kills a background process to reclaim memory and later restores your task, it creates a new process and a new Application before recreating the activity. Anything you stored only in memory is gone, while saved instance state and persistent storage remain. Treat the Application instance as a convenient entry point, not as storage.
Yes. Application.onCreate() runs once per process, before any activity, service or receiver is created.
Use the application property, or applicationContext when you only need a Context. Cast it to your own class only when you really need custom members.
You can, but you should not for important data. Process death resets them. Use a ViewModel for screen state and Room or DataStore for persistent data.
A heavy onCreate() does. Keep it light and move noncritical setup to background threads or App Startup.
The Application class is the process-level entry point. Subclass it, register it in the manifest, and use onCreate() for light, one-time setup such as dependency injection, logging and notification channels. Avoid heavy work and global mutable state there, because the system can restart the process at any time and every start runs your code.