Android application in-depth analysis

Source: Internet
Author: User

Now a lot of mature project framework has adopted a relatively good design, one of which is the use of application, then application in the end what role on Android?

what is application
Application and activity, Service is a system component of the Android framework, and when the Android program is launched, a Application object is created 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 in the Manifest Application tag register ( Just add a name attribute to the application tag to set the name of your application.
Android will create a application class object for each program runtime and create 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.

Data passing between components using application
If there is an activity a, jump to activity B, and need to recommend some data, the usual practice is intent.putextra () let intent carry, Or there is a bundle to add information to the bundle so that intent recommend bundle objects for 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 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.

Data Caching in Application
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.
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.

memory leaks in Java are only, some (some) The object is no longer being used and should be reclaimed by GC, but one object holds a reference to this object and prevents the 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.

normally, when the user rotates 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 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.
Some of the reasons that often lead to memory leaks:
Keeping a long-lived reference to a context. holds a context object so that the GC cannot be reclaimed.   
1, a view, scoped beyond the scope of the activity, such as a static view or a view cache into the application etc
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 which the activity is reclaimed by the system, but the task of Runnalbe is not completed and a reference to the activity is released. 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 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 to write the inner class if you do not need the current referencestatic or, extract the inner class into a separate class, or avoid the scope of the inner object scope beyond 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.

pass data through application between two activity Remember to remove the data stored in the application HashMap after the data transfer is complete in order to avoid a memory leak.

reference: HTT p://

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: 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.