Android Application Object

Source: Internet
Author: User

From http://www.cnblogs.com/elleniou/archive/2012/05/16/2502661.html

What is application
Application is a system component of the android framework like activity and service. Program At startup, the system creates an application object to store some information about the system. Generally, we do not need to specify an application. In this case, the system automatically creates an application for us. If you need to create your own application, it is also easy to create a class that inherits the application and registers it in the application tag of manifest (you only need to add a name attribute to the application tag to set the name of your application ).
The Android system creates only one application Class Object for each program running. Therefore, the application is a class in singleton mode. 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 activities and services are all the same object. Therefore, you can use the application to perform operations such as data transmission, data sharing, and data caching.
Data passing between components using Application
Suppose there is activity A, jump to Activity B, and recommend some data. The common practice is intent. putextra () allows intent to carry, or a bundle adds information to the bundle so that intent recommends bundle objects for transmission. However, there is a problem in this process: intent and bundle can carry some basic data types. If you want to implement complex data transfer, it will be more troublesome, serializable or parcellable interfaces are usually required. This is actually an IPC data transmission method for Android. If our two activities are in the same process, why is it so troublesome? You just need to pass the reference of the object to be passed in the past.
The basic idea is this. Create a hashmap in the application, with the string as the index, and the object as the value so that our hashmap can store any type of objects. Put the objects to be passed into this hashmap in activity A, and then pass the string indexed by this person to Activity B through intent or other methods, activity B can retrieve this object in hashmap based on this string. You only need to go down to the next type to transfer the object.
Data caching in Application
I usually get used to creating two hashmaps in the application, one for data transmission and the other for slowing down some data. For example, an activity needs to obtain some data from the website. After obtaining the data, we can cache the data to the application. When the page is set to another activity, you can directly use the cached data. But if you need to cache a large amount of data, it is best to cache some soft references) softreference, and cache the data to the local Rom or SD card. If the cache in the application does not exist, search from the local cache. If the locally cached data does not exist, it will be retrieved from the network.
Pitfalls
If an application is used to save objects that are not supposed to be saved, memory leakage may easily occur. If you perform time-consuming operations in oncreate of the application, the startup time of the program will be directly affected. You cannot use onterminate for cleanup. Because android will try to keep your program running, it is very likely that onterminate will not be called.
Memoryleak
In Java, memory leakage occurs only when a (some) object is no longer in use and should be recycled by GC, but an object holds a reference of this object to prevent this object from being recycled. For example, we usually create a view textview TV = new textview (this) like this; here this is usually activity. Therefore, this textview holds the reference of this activity. See the figure below (copied in Google Io 2011 ppt)

Generally, when a user turns the mobile phone, Android will call the oncreate () method again to generate a new activity. The original activity should be recycled by GC. However, if an object such as a view has a larger scope than this activity (for example, there is a static object or we include the reference of this view in the application ), at this time, the original activity cannot be recycled by GC, and the activity itself holds a lot of object references, so the memory of the entire activity is leaked.
Some causes of frequent memory leaks:
Keeping a long-lived reference to a context. The object that holds a context cannot be recycled by GC.
1. The scope of a view exceeds the scope of the activity. For example, a static view or a view is cached in the application. etc
2. Some drawable scopes associated with the view are beyond the scope of the activity.
3. runnable object: for example, a new thread is enabled in an activity to execute a task, during which the activity is recycled by the system, however, the runnalbe task has not been completed and the reference of the activity is leaked. However, this leakage generally occurs for a period of time, and only the runnalbe thread has completed the execution, this activity can be recycled again.
4. The scope of objects in the memory class is beyond the scope of the activity. For example, a memory class is defined to store data, and the memory class object is passed to other activities or services. Because the object of the internal class holds the reference of the current class, it also holds the reference of context. The solution is to write the internal class
Static or, extract the internal class into a separate class, or avoid the internal object scope exceeding the activity scope.
Out of memery error in Android, the memory size allocated to each program is limited. If this number is exceeded, an out of memory error is reported. The memory size allocated by Android to the program is related to the mobile phone hardware. The following figure shows the data of some mobile phones:
G1: 16 m droid: 24 Nexus One: 32 m xoom: 48 Ms
So try to cache some big data in the program to a local file. To avoid excessive memory usage.
Snippets
1. Transfer Data between two activities through application

Remember to remove the data stored in the hashmap of the application after the data is transferred to avoid Memory leakage.

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.