to top
Android APIs
public class

Application

extends ContextWrapper
implements ComponentCallbacks2
java.lang.Object
   ↳ android.content.Context
     ↳ android.content.ContextWrapper
       ↳ android.app.Application
Known Direct Subclasses

Class Overview

Base class for those who need to maintain global application state. You can provide your own implementation by specifying its name in your AndroidManifest.xml's <application> tag, which will cause that class to be instantiated for you when the process for your application/package is created.

There is normally no need to subclass Application. In most situation, 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), the function to retrieve it can be given a Context which internally uses Context.getApplicationContext() when first constructing the singleton.

Summary

Nested Classes
interface Application.ActivityLifecycleCallbacks  
interface Application.OnProvideAssistDataListener Callback interface for use with registerOnProvideAssistDataListener(Application.OnProvideAssistDataListener) and unregisterOnProvideAssistDataListener(Application.OnProvideAssistDataListener)
[Expand]
Inherited Constants
From class android.content.Context
From interface android.content.ComponentCallbacks2
Public Constructors
Application()
Public Methods
void onConfigurationChanged(Configuration newConfig)
Called by the system when the device configuration changes while your component is running.
void onCreate()
Called when the application is starting, before any activity, service, or receiver objects (excluding content providers) have been created.
void onLowMemory()
This is called when the overall system is running low on memory, and actively running processes should trim their memory usage.
void onTerminate()
This method is for use in emulated process environments.
void onTrimMemory(int level)
Called when the operating system has determined that it is a good time for a process to trim unneeded memory from its process.
void registerActivityLifecycleCallbacks(Application.ActivityLifecycleCallbacks callback)
void registerComponentCallbacks(ComponentCallbacks callback)
Add a new ComponentCallbacks to the base application of the Context, which will be called at the same times as the ComponentCallbacks methods of activities and other components are called.
void registerOnProvideAssistDataListener(Application.OnProvideAssistDataListener callback)
void unregisterActivityLifecycleCallbacks(Application.ActivityLifecycleCallbacks callback)
void unregisterComponentCallbacks(ComponentCallbacks callback)
Remove a ComponentCallbacks object that was previously registered with registerComponentCallbacks(ComponentCallbacks).
void unregisterOnProvideAssistDataListener(Application.OnProvideAssistDataListener callback)
[Expand]
Inherited Methods
From class android.content.ContextWrapper
From class android.content.Context
From class java.lang.Object
From interface android.content.ComponentCallbacks
From interface android.content.ComponentCallbacks2

Public Constructors

public Application ()

Added in API level 1

Public Methods

public void onConfigurationChanged (Configuration newConfig)

Added in API level 1

Called by the system when the device configuration changes while your component is running. Note that, unlike activities, other components are never restarted when a configuration changes: they must always deal with the results of the change, such as by re-retrieving resources.

At the time that this function has been called, your Resources object will have been updated to return resource values matching the new configuration.

For more information, read Handling Runtime Changes.

Parameters
newConfig The new device configuration.

public void onCreate ()

Added in API level 1

Called when the application is starting, before any activity, service, or receiver objects (excluding content providers) have been created. Implementations should be as quick as possible (for example using lazy initialization of state) since the time spent in this function directly impacts the performance of starting the first activity, service, or receiver in a process. If you override this method, be sure to call super.onCreate().

public void onLowMemory ()

Added in API level 1

This is called when the overall system is running low on memory, and actively running processes should trim their memory usage. While the exact point at which this will be called is not defined, generally it will happen when all background process have been killed. That is, before reaching the point of killing processes hosting service and foreground UI that we would like to avoid killing.

You should implement this method to release any caches or other unnecessary resources you may be holding on to. The system will perform a garbage collection for you after returning from this method.

Preferably, you should implement onTrimMemory(int) from ComponentCallbacks2 to incrementally unload your resources based on various levels of memory demands. That API is available for API level 14 and higher, so you should only use this onLowMemory() method as a fallback for older versions, which can be treated the same as onTrimMemory(int) with the TRIM_MEMORY_COMPLETE level.

public void onTerminate ()

Added in API level 1

This method is for use in emulated process environments. It will never be called on a production Android device, where processes are removed by simply killing them; no user code (including this callback) is executed when doing so.

public void onTrimMemory (int level)

Added in API level 14

Called when the operating system has determined that it is a good time for a process to trim unneeded memory from its process. This will happen for example when it goes in the background and there is not enough memory to keep as many background processes running as desired. You should never compare to exact values of the level, since new intermediate values may be added -- you will typically want to compare if the value is greater or equal to a level you are interested in.

To retrieve the processes current trim level at any point, you can use ActivityManager.getMyMemoryState(RunningAppProcessInfo).

Parameters
level The context of the trim, giving a hint of the amount of trimming the application may like to perform. May be TRIM_MEMORY_COMPLETE, TRIM_MEMORY_MODERATE, TRIM_MEMORY_BACKGROUND, TRIM_MEMORY_UI_HIDDEN, TRIM_MEMORY_RUNNING_CRITICAL, TRIM_MEMORY_RUNNING_LOW, or TRIM_MEMORY_RUNNING_MODERATE.

public void registerActivityLifecycleCallbacks (Application.ActivityLifecycleCallbacks callback)

Added in API level 14

public void registerComponentCallbacks (ComponentCallbacks callback)

Added in API level 14

Add a new ComponentCallbacks to the base application of the Context, which will be called at the same times as the ComponentCallbacks methods of activities and other components are called. Note that you must be sure to use unregisterComponentCallbacks(ComponentCallbacks) when appropriate in the future; this will not be removed for you.

Parameters
callback The interface to call. This can be either a ComponentCallbacks or ComponentCallbacks2 interface.

public void registerOnProvideAssistDataListener (Application.OnProvideAssistDataListener callback)

Added in API level 18

public void unregisterActivityLifecycleCallbacks (Application.ActivityLifecycleCallbacks callback)

Added in API level 14

public void unregisterComponentCallbacks (ComponentCallbacks callback)

Added in API level 14

Remove a ComponentCallbacks object that was previously registered with registerComponentCallbacks(ComponentCallbacks).

public void unregisterOnProvideAssistDataListener (Application.OnProvideAssistDataListener callback)

Added in API level 18