Application class

The Application class

Application, like Activity and Service, is a system component of the Android framework. When the Android program starts, the system creates an Application object to store some information about the system.

The Android system automatically creates an object of the Application class for each program runtime and only one is created, so Application can be said to be a class in the singleton mode.

Usually we don't need to specify an Application, the system will automatically create it for us, if you need to create your own Application, it's very simple! Create a class that inherits Application and register it in the application tag in the AndroidManifest.xml file (just add the name attribute to the application tag and add your own Application name).

When the Application is started, the system will create a PID, that is, the process ID, and all activities will run on this process. Then we initialize the global variables when the Application is created, and all Activities of the same application can get the values ​​of these global variables. In other words, if we change the values ​​of these global variables in an Activity, then in the same application The value of other Activity will change.

The life cycle of the Application object is the longest in the entire program, and its life cycle is equal to the life cycle of the program. Because it is a global singleton, the objects obtained in different Activity and Service are the same object. Therefore, some operations can be performed through Application, such as: data transfer, data sharing and data caching.

Application scenarios:

In Android, application-level global variables can be implemented by inheriting the Application class. This global variable method is more secure than static classes, and will not be released until all activities of the application are destroyed.


Reprinted: http://www.cnblogs.com/renqingping/archive/2012/10/24/Application.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325862105&siteId=291194637