Application
public
class
Application
extends ContextWrapper
implements
ComponentCallbacks2
| java.lang.Object | |||
| ↳ | android.content.Context | ||
| ↳ | android.content.ContextWrapper | ||
| ↳ | android.app.Application | ||
|
Known direct subclasses
|
Base class for maintaining global application state. You can provide your own
implementation by creating a subclass and specifying the fully-qualified name
of this subclass as the "android:name" attribute in your
AndroidManifest.xml's <application> tag. The Application
class, or your subclass of the Application class, is instantiated before any
other class when the process for your application/package is created.
Note: There is normally no need to subclass
Application. In most situations, 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), include
Context.getApplicationContext()
as a Context argument when invoking your singleton's
getInstance() method.
Summary
Nested classes | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
interface |
Application.ActivityLifecycleCallbacks
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
interface |
Application.OnProvideAssistDataListener
Callback interface for use with |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Inherited constants | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
From class
android.content.Context
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
From interface
android.content.ComponentCallbacks2
|
Public constructors | |
|---|---|
Application()
|
|
Public methods | |
|---|---|
static
String
|
getProcessName()
Returns the name of the current process. |
void
|
onConfigurationChanged(Configuration newConfig)
Called by the system when the device configuration changes while your component is running. If you override this method you must call through to the superclass implementation. |
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. If you override this method you must call through to the superclass implementation. |
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. If you override this method you must call through to the superclass implementation. |
void
|
registerActivityLifecycleCallbacks(Application.ActivityLifecycleCallbacks callback)
|
void
|
registerComponentCallbacks(ComponentCallbacks callback)
Add a new |
void
|
registerOnProvideAssistDataListener(Application.OnProvideAssistDataListener callback)
|
void
|
unregisterActivityLifecycleCallbacks(Application.ActivityLifecycleCallbacks callback)
|
void
|
unregisterComponentCallbacks(ComponentCallbacks callback)
Remove a |
void
|
unregisterOnProvideAssistDataListener(Application.OnProvideAssistDataListener callback)
|
Inherited methods | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
From class
android.content.ContextWrapper
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
From class
android.content.Context
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
From class
java.lang.Object
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
From interface
android.content.ComponentCallbacks2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
From interface
android.content.ComponentCallbacks
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Public constructors
Public methods
getProcessName
public static String getProcessName ()
Returns the name of the current process. A package's default process name is the same as its package name. Non-default processes will look like "$PACKAGE_NAME:$NAME", where $NAME corresponds to an android:process attribute within AndroidManifest.xml.
| Returns | |
|---|---|
String |
|
onConfigurationChanged
public void onConfigurationChanged (Configuration newConfig)
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. If you override this method you must call through to the superclass implementation.
| Parameters | |
|---|---|
newConfig |
Configuration: This value cannot be null. |
onCreate
public void onCreate ()
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().
Be aware that direct boot may also affect callback order on
Android Build.VERSION_CODES.N and later devices.
Until the user unlocks the device, only direct boot aware components are
allowed to run. You should consider that all direct boot unaware
components, including such ContentProvider, are
disabled until user unlock happens, especially when component callback
order matters.
If you override this method you must call through to the superclass implementation.
onLowMemory
public void onLowMemory ()
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 ComponentCallbacks2#onTrimMemory 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 ComponentCallbacks2#onTrimMemory with the ComponentCallbacks2#TRIM_MEMORY_COMPLETE level.
onTerminate
public void onTerminate ()
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.
If you override this method you must call through to the
superclass implementation.
onTrimMemory
public 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. 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).
If you override this method you must call through to the
superclass implementation.
| Parameters | |
|---|---|
level |
int: The context of the trim, giving a hint of the amount of
trimming the application may like to perform.
Value is ComponentCallbacks2.TRIM_MEMORY_COMPLETE, ComponentCallbacks2.TRIM_MEMORY_MODERATE, ComponentCallbacks2.TRIM_MEMORY_BACKGROUND, ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN, ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL, ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW, or ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE |
registerActivityLifecycleCallbacks
public void registerActivityLifecycleCallbacks (Application.ActivityLifecycleCallbacks callback)
| Parameters | |
|---|---|
callback |
Application.ActivityLifecycleCallbacks |
registerComponentCallbacks
public 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. Note that you
must be sure to use unregisterComponentCallbacks(ComponentCallbacks) when
appropriate in the future; this will not be removed for you.
| Parameters | |
|---|---|
callback |
ComponentCallbacks: The interface to call. This can be either a
ComponentCallbacks or ComponentCallbacks2 interface. |
registerOnProvideAssistDataListener
public void registerOnProvideAssistDataListener (Application.OnProvideAssistDataListener callback)
| Parameters | |
|---|---|
callback |
Application.OnProvideAssistDataListener |
unregisterActivityLifecycleCallbacks
public void unregisterActivityLifecycleCallbacks (Application.ActivityLifecycleCallbacks callback)
| Parameters | |
|---|---|
callback |
Application.ActivityLifecycleCallbacks |
unregisterComponentCallbacks
public void unregisterComponentCallbacks (ComponentCallbacks callback)
Remove a ComponentCallbacks object that was previously registered
with registerComponentCallbacks(android.content.ComponentCallbacks).
| Parameters | |
|---|---|
callback |
ComponentCallbacks |
unregisterOnProvideAssistDataListener
public void unregisterOnProvideAssistDataListener (Application.OnProvideAssistDataListener callback)
| Parameters | |
|---|---|
callback |
Application.OnProvideAssistDataListener |
Content and code samples on this page are subject to the licenses described in the Content License. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2020-09-30 UTC.