You’re almost there — sign up to start building in Notion today.
Sign up or login
Application Subclass

Application Subclass

In Android, the Application subclass is a base class that represents the application itself. It is an integral part of the Android application lifecycle and serves as the entry point for your application. The Application subclass allows you to maintain global application state and perform initialization tasks that need to be executed before any other components of your app.
To create an Application subclass, you need to create a new class that extends the
android.app.Application
class. Here's an example:
public class MyApp extends Application { @Override public void onCreate() { super.onCreate(); // Perform application initialization tasks here // Set up global variables, initialize libraries, etc. } // Add any additional methods or variables as needed }
To make your custom Application subclass known to the Android system, you need to update the
AndroidManifest.xml
file. Find the
<application>
element and add the
android:name
attribute with the fully qualified name of your custom Application subclass:
<manifest xmlns:android="<http://schemas.android.com/apk/res/android>" package="com.example.myapp"> <application android:name=".MyApp" ... > ... </application> ... </manifest>
By using an Application subclass, you can manage and share application-level data and resources across various components of your app, such as activities, services, and content providers. It provides a convenient way to initialize and maintain global state throughout the lifecycle of your application.
Android applications can define a subclass of Application. Applications can, but do not have to define a custom subclass of Application. If an Android app defines a Application subclass, this class is instantiated prior to any other class in the application.
If the 
attachBaseContext
 method is defined in the Application subclass, it is called first, before the 
onCreate
 method.