Application class in Android

| Page Views: 1428

Android provides a base class in the 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 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?

To learn more android components, check here.

Application class Methods

Here are some public methods in the

  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.

About Me.

After completing his Software Engineering bachelors program, Oclemy(Clement Ochieng) these days is a man of two lives. At day he works for a startup in Nairobi, Kenya. At night he works tirelessly on building ProgrammingWizards TV, a tv channel for student coders and this website to help share the source code. In between he practices Meditation and Self actualization to help him keep balance. He also likes going for long solo walks to connect more with nature.


What do You Think

Previous Post Next Post