1. Application Class
Application, like Activity,service, is a system component of the Android framework that creates a application object to store some information about the system when the Android program starts.
Usually we do not need to specify a application, then the system will automatically help us to create, if you need to create your own application, you need to: Create a class to inherit application and register in the Application tab of manifest.
2. Characteristics of the Application class
<1>. Single case:
The Android system creates an object of the application class for each program runtime and creates only one, so application can be said to be a class of Singleton (singleton) mode.
<2> the longest life cycle:
The life cycle of an Application object is the longest in the entire program, and its life cycle is equal to the program's life cycle. Because it is a global singleton, the objects obtained in different activity,service are the same object.
3. Role of the application class
Based on the characteristics of application class, we can perform some operations, such as data transfer, data sharing, data caching, etc. through application.
4. Life cycle of the application class
<1>. onCreate When you create an application
<2>. Onterminate is called when the Application object is terminated, is not guaranteed to be called, when the program is terminated by the kernel to release resources for other applications, that
Will not be reminded, and the process is terminated directly without invoking the Onterminate method of the application's object
<3>. Onlowmemory This method is called when the daemon has terminated the resource and is scarce. A good application typically frees up some unnecessary
Resources to cope with situations where the daemon has terminated and the foreground application memory is not enough.
<4>. This method is triggered when the onconfigurationchanged configuration changes
<5>. Remark: Application killed situation analysis:
To decide which process to kill when memory is low, Android divides the process into a "level of importance" based on the components and their state that are running within those processes. The degree of importance is sorted by the following rules:
1 A front-end process can be a process that holds the activity that runs on the screen at the forefront and interacts with the user (when the Onresume method is invoked), or it can hold a running Intentreceiver ( That is, he is performing his own onreceiveintent method) process. In the system, there will be only a few such processes, and unless the memory is low enough for these processes to run, the system will not actively kill those processes. At this point, the device usually has reached a state that requires memory grooming, so the process is killed in order not to let the user interface stop responding.2 A visual process is a process that holds an activity that is visible to the user but not displayed at the forefront (when the OnPause method is called). For example, this process usually occurs in a front end activity that appears in a dialog box and keeps the previous activity visible. This process is considered extremely important by the system and is usually not killed unless the visible processes are killed in order to keep all the front-end processes running properly.3 A service process is a process that holds one service, which is initiated by the StartService () method, although the users of these processes are not directly visible, but usually they do work with a user who is very concerned (for example, playing mp3 in the background or downloading an upload file in the background) ), so that the system does not kill the service process until all the front-end processes and visual processes are running properly.4 A background process is a process that holds an activity that is no longer visible to the user (the OnStop () method is called). These processes do not directly affect the user experience. Joining these processes is complete, completing their lifecycle correctly (visit activity to see more details), The system kills these background processes at any time when memory is freed for the first three processes. There are usually a lot of background processes running, so these processes are stored in an LRU list to ensure that the last process seen by the user is finally killed when the memory is low.5An empty process is a process that does not hold any active application components. The only reason to keep this process is to provide a caching mechanism that shortens the startup time of his app's next run. In itself, the system kills these processes to balance the resources of the entire system between these empty processes and the underlying core caches.
When a process needs to be categorized, the system will select one of the most important levels in all components that are active in the process as a classification basis. Review the documentation for activity, Service, and intentreceiver to see how each component contributes throughout the life cycle of the process. Each classes document describes in detail their role in the life cycle of their respective applications.
5. Implementing the Application Class
<1>. Inherit application
Public classCustomApplicationextendsapplication{Private Static FinalString VALUE = "Harvey";PrivateString value; @Override Public voidonCreate () {Super. OnCreate (); SetValue (VALUE);//Initializing global Variables} Public voidSetValue (String value) { This. Value =value;} PublicString GetValue () {returnvalue;}}
Note: Inheriting the application class, the main rewrite of the OnCreate () method (Android.app.Application package OnCreate () is the real Android program entry point), is created when the value of the initialization variable. The variable can then be manipulated in various files throughout the application.
<2>. Configuring custom application in the Applicationmanifest.xml file
<applicationandroid:name= "CustomApplication" ></application>
6. Using the application class to pass data
If there is an activity a, jump to activity B, and need to recommend some data, it is usually intent.putextra () let intent carry, or have a bundle to add information to the bundle so intent recommend bundle object To implement delivery.
However, one problem with this is that the data types that intent and bundles can carry are basic data types that are cumbersome to implement for complex data passing, and typically require Serializable (serialized objects) or parcellable ( is an Android-specific feature that is efficient than implementing the serializable interface and can be used for intent data transfer or for interprocess communication (IPC)) interfaces. This is actually an IPC data transfer method for Android.
If the two of our activity is in the same process, why bother, just pass the reference to the object that needs to pass through.
The basic idea is this: Create a hashmap in application, with the string as the index, and object as value so that our HashMap can store any type of object. In activity A, you put the object that needs to be passed in the HashMap, and then pass the index string to activity B through intent or other passes, and activity B can take the object out of the HashMap based on the string. As long as the downward turn of the type, the implementation of the object's delivery.
7. Using the application class to cache data
Typically, two hashmap are created in application, one for data transfer and one for caching some data.
For example, there is an activity need to get some data from the website, we can put this data into the application, when the page is set to other activity back, we can directly use the cached data. However, if you need to cache some large amounts of data, it is best to cache some (soft reference) SoftReference, and the data cache to the local ROM or SD card. If the cache in application does not exist, it is looked up from the local cache if the locally cached data does not exist and is fetched from the network.
20151008_android Application Class