Application class in Android


Android provides a base class in the android.app package which can be used to maintain the global state of an application.

Obviously activities are used extensively in almost all applications as they are a core component of android platform. However, an activity can only keep an activity state.

If the system or you destroy an activity then that state is lost. So android.app.Application provide a way for us to maintain state that is global to all activities.

To provide your own implementation of the Application class, you specify the the name in the <application> tag inside your android_manifest.xml file. Doing this ensures that during the creation of your application's process, the Application gets instantiated automatically for you.

However, when it comes to maintaining global state, it's recommended to use static singletons whenever possible since they are more modular.

Introduction to Android Components

Application is an android component. However there are 4 other components. But first what is an android component?

Android Components are the building blocks of an Android app.

Android has around 5 types of components:

  1. Activity
  2. Service
  3. BroadcastReceiver
  4. ContentProviders
  5. Application.

Each of these components plays a specific role in an Android app which serves a distinct purpose and has distinct life-cycles(the flow of how and when the component is created and destroyed).

Activities are responsible for user interface. Normally you start by creating and activity, or the IDE generating for you a MainActivity.

Services implement long running operations in the background.

BroadcastReceivers listen for System events.

ContentProviders on the other hand are used to store the application data.

All the above have to be registered in the AndroidManifest.xml file.

The above components get tied up together by use of Intents.

Application is also an android component albeit one that is rarely used. Applications provide and maintain a global state to all the stateful components like Activities and Services.

To learn more android components, check here.

Application class Methods

Here are some public methods in the android.app.Application.

  1. onCreate() : This method gets called when tha application has been created and is starting. By this time still there is no actiity, service or receiver objects(excluding content providers) created. You have to call the onCreate() method of the super Application class: super.OnCreate().
  2. onConfigurationChanged() :This method will be called by the system when the device configuration changes are detected. Only activities get restarted when this occurs. Other components don't and have to deal with the consequences of this change such as re-fetching resources they were using.
  3. onLowMemory() : The android system will call this method when it's running low on memory. By this time the memory is low and actively running process need to trim their memory usage. This can happen at any time with no specific defined time. However by this time probably all background processes have already been killed. Typically you implement this so as to release caches and other non-critical resources you are occupying.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think

Dear readers drop us your comments below. We are building a community of students and learners. Start by dropping us your suggestions below. What tutorials do you want us to do for example? Where can we improve? What are some awesome resources out there? Do you have any code you want to share with us?
By the way that example or snippet you have lying in your computer can really help beginner programmers. We can share it here with other students.

Previous Post Next Post