Android learning Notes-Application Object introduction, usage and precautions

Source: Internet
Author: User

Introduction to appliation objects

Application is a system component of the android framework like activity and service.ProgramAt 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 write your application name ).

The Android system creates only one application class object when each program is 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 global, the objects obtained in different activities and services are the same object. Therefore, the application is suitable for data transmission, data sharing, data caching, and other operations.

Transmit data between components

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 to add information to the bundle, so that intent recommends the bundle object 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 two of our activities are in the same
Why is it so troublesome in the process? You only 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 index string 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 Cache

I usually get used to creating two hashmaps in the application, one for data transmission and the other for caching 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. However, if the amount of data to be cached is large, it is best to cache some soft references 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
.

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 is directly affected. Some cleanup tasks cannot be completed by onterminate. Because android will try its best to keep your program running, it is very likely that onterminate will not be called.

Memory leakage (memoryleak)

In Java, memory leakage means that a (some) object is no longer used 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); here this is usually activity. Therefore, this textview holds the reference of this activity.
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 as static without the current reference, 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. Remember to remove the data stored in the hashmap of the application after the data is transferred to avoid Memory leakage.

 

Citation address:

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

Reference: http://blog.chinaunix.net/uid-23392298-id-3269059.html

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.