Android Application Life cycle Introduction

Source: Internet
Author: User

See application This class, always do not know what this is for, today just a bit of time, so carried out the detailed study.

First, the overall concept of its interpretation:

The description of him in the Android source code is;

* Base class for those who need to maintain global application state. You can
* Provide your own implementation by specifying it name in your
* androidmanifest.xml ' s <application> tag, which'll cause that class
* To being instantiated for if the process for your application/package is
* created.

Description in SDK: The application class is for those basic classes that need to preserve the design of global variables, and you can implement your own in the Androidmanifest.xml <application> tags, and the result is: when your Application or a package is created will cause that class to be built.

Understanding: That is, application is used to preserve global variables, and is then present when the package is created. So when we need to create global variables, we do not need to create the static variables of public permissions as J2SE, but directly in the application. Just call the context's Getapplicationcontext or activity's Getapplication method to get a Application object, and then do the appropriate processing.



For example, in the Launcher module, it wrote a application itself and set it up in Androidmanifest.xml:

<application
Android:name= "Com.android.launcher2.LauncherApplication"

For his settings, refer to this module.


Two. Description of the method inside:

OnCreate ();

/**
* Called when the application are starting, before any other application
* Objects has 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, being sure to call Super.oncreate ().
*/

This function is called when our application is started and is created earlier than other objects in the application, and this implementation is as fast as possible because this time directly affects our first Activity/service

/receiver. If you want to override this method you must call Super.oncreate ().

Onterminate ():

/**
* This method was for use in emulated process environments. It would
* Never is called on a production Android device, where processes is
* Removed by simply killing them; No user code (including this callback)
* is executed when doing so.
*/

This function simulates a process environment and will never be called in a real machine.


Android Application object must be mastered by seven points

captain Real . updated on July 01 Access (1589) Comments (4)

What is 1:application?
1
      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 their own application, it is easy to create a class to inherit application and register in manifest application tag (just add a name attribute to the application tag to set the name of your application).

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 singleton (singleton) A class of patterns. And the life cycle of the Application object is the longest in the whole 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,service are the same object. So through the application to do some, data transfer, data sharing, such as data caching operations.

2: Passing Data through application
12  
     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 transfer, and often require the implementation of serializable or parcellable 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 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.
3:application Data Cache

I usually get used to building two HashMap 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.

4:pitfalls (Chinese: easy to make mistakes)

Using application if you save some objects that you shouldn't save, you can easily cause a memory leak. If you perform a more time-consuming operation in the application OnCreate, the startup time of the program will be directly affected. No cleanup work can be done with Onterminate because Android will try to keep your program running, so it's likely that onterminate won't be called.

5:memoryleak

Usually, when the user turns the phone, Android will re-call the OnCreate () method to generate a new activity, and the original activity should be recycled by the GC. But if there is an object such as a view that has more scope than the activity (such as having a static object or we put the view reference in the application), the original activity will not be recycled by the GC. The activity itself holds many references to objects, so the entire activity's memory is compromised.

123  
 Note: often leads to memory leaks core causes: keeping a long-lived reference to a context. Holding a Context object  , so the GC cannot be recycled. The situation is as follows:  

1 The scope of a view is beyond the scope of the activity in which it is located, such as a static view or a view cache to the application etc
Understanding: Memory: Attention to static data and data in cache, attention release,
2. Some drawable that are associated with the view are scoped beyond the scope of the activity.
3.runnable object: For example, a new thread is enabled in an activity to perform a task, During this time the activity was reclaimed by the system, but Runnalbe's task has not been completed and the activity of the reference to the leak, but this leakage is generally leaked for a while, only the runnalbe thread execution closed, the activity can be normal recycling.

4. The object scope of the memory class is beyond the scope of the activity: for example, a memory class is defined to store the data, and the object of the memory class is passed to other activity or service. Because an object in an inner class holds a reference to the current class, it also holds a reference to the context. The workaround is if you do not need the current reference to write the inner class as static or to extract the inner class into a separate class, or to avoid the scope of the inner object beyond the scope of the activity. Out of Memery error The amount of memory that is divided by each program in Android is limited, and if it exceeds this number, it will be reported out of the error. The amount of memory that Android assigns to programs is related to the phone's hardware, and here are some of the phone's data:
g1:16m droid:24 Nexus one:32m xoom:48ms
So try to cache some of the big data in the program to a local file. Avoid exceeding the amount of memory used.
Remember to remove the data stored in the application HashMap after the data has been transmitted to avoid memory leaks

6: Life cycle:

OnCreate When you create an application
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
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.
This method is triggered when the onconfigurationchanged configuration changes

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: The front-end process can be a process that holds the activity that runs on the screen at the forefront and interacts with the user (the Onresume method is called), 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: The visual process is a process that holds an activity that is visible to the user but does not show up in the front-end (when the OnPause method is invoked). 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: The service process is a process that holds the services, which are initiated by the StartService () method, although the users of these processes are not directly visible, but usually the work users they do are very concerned (for example, playing mp3 in the background or downloading the uploaded files in the background), Therefore, the system will 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 invoked). These processes do not directly affect the user experience. Joining these processes is complete, completing their lifecycle correctly (accessing the activity to see more details), and the system will kill these background processes at any time when freeing memory 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.
5: An 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. Www.2cto.com
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.

# 7:application's Context #
1, it describes the information of an application environment, that is, the context.
2. The class is an abstract class, and Android provides a concrete implementation class for that abstract class (we'll talk about the Contextiml class later).
3, through it we can get the resources and classes of the application, also include some application-level operations, such as: Start an activity, send a broadcast, accept intent
Information, etc...


Android Application Life cycle Introduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.