Android Application object must be mastered by seven points

Source: Internet
Author: User

What is 1:application?

          application and Activity,service are a system component of the Android framework, and when the Android program starts, the system creates a Application object that is used to store some information about the system. 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: Pass data through application           If you have an activity a, jump to activity B and need to recommend some data, the usual practice is Intent.putextra () let intent carry, or have a bundle to add information to bundle let intent recommend bundle object, implement pass. 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 caching 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: fallible 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 in Java memory leaks are only, some (some) objects are no longer being used should be recycled by GC, but there is an object holding a reference to this object to prevent this object from being recycled. For example, we typically create a view TextView TV = new TextView (this), where this is usually activity. So this textview holds the reference to the activity. Here's a picture (copied in Google IO ppt) Normally, when the user turns the phone, Android calls 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 objectOr we put this view reference in the application), when the original activity will not be collected by the GC, the activity itself holds many references to objects, so the entire activity of memory is leaked.        Remarks: Often causes memory leaks core causes:       keeping a long-lived reference to a Context. Hold a cont Ext object so that the GC cannot be recycled.       The following:     1. A view is scoped beyond the scope of the activity, such as a static view or a view cache to the application etc        Understanding: Memory: Pay attention to the data in static data and cache; Note Release;  2. Some scopes of drawable associated with the view are outside the scope of the activity.          3.runnable objects: For example, a new thread is enabled in an activity to perform a task, during which time the activity is reclaimed by the system, but Runnalbe's   The task has not been completed and has a reference to the activity has been leaked, but this leakage is generally leaked for a period of time, only runnalbe thread execution closed, the activity can be recycled.    4. The object scope of the memory class goes 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 the following 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 data stored in application HashMap after data transfer to avoid memory leaks      6: life cycle:   OnCreate created when an application is created   Onterminate is called when the Application object is terminated, is not guaranteed to be called, and when the program is terminated by the kernel to release resources for other applications, it will not be alerted, And does not invoke the Onterminate method of the object of the application and terminates the input   process directly   Onlowmemory This method is called when the daemon has terminated the resource and is scarce. A good application typically frees up unnecessary resources in this method to cope with situations where the daemon has terminated and the foreground application memory is not enough.   Trigger This method when onconfigurationchanged configuration changes   Remarks: Application killed case analysis:        to decide which process to kill at low memory , Android divides the process into a "level of importance" based on the components that are running within these processes and their status. Its importance is sorted by the following rules:  1: A front-end process can be a process that holds the activity that runs at the front of the screen 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 not shown at 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 it is necessary to keep all front-end processes running properlyKill these visible processes. 3: The service process is a process that holds a single service, which is 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, Play mp3 in the background or download   upload files in the background, so unless the system is running properly for all front-end processes and visual processes, it will not kill the service process. 4: The background process is holding an activity that is no longer visible to the user (the OnStop () method is invoked) The process. 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: The 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 context        1, It describes the information of an application environment, which is the context.         2, this 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 which we can get the resources and classes of the application, as well as some application-level actions, such as: Start an activity, send a broadcast, accept intent      information, etc.

Seven points that the Android application object must know

Related Article

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.