Apps for Android in application

Source: Internet
Author: User

When the Android program starts, the system creates a Application object 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.

From the Android SDK documentation, we know that the Android app is typically constructed from the following four components: Activity,broadcast Intent Receiver, service, content provider Provider). We can use the following diagram to represent the concept space of Android. These components are attached to the application, and the application is not built in the first place, but when the components are set up and need to run before the application objects are built.

650) this.width=650; "title=" image "Src=" http://hi.csdn.net/attachment/201005/24/0_127471503710zt.gif "alt=" image " Width= "616" height= "473" border= "0" style= "margin:0px;padding:0px;border:0px;"/>

Another article about http://blog.csdn.net/lieren666/article/details/7598288

Data passing between components using application

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<string,object> 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<string,object> 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.

Pitfalls

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.

Memoryleak

In Java memory leaks are only, some (some) objects are no longer being used should be reclaimed by GC, but one object holds a reference to this object and prevents 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. See a picture below (copy in Google IO ppt)

650) this.width=650; "Title=" Android Notes <wbr>application object Usage-data transfer and memory leak issues "src=" http://pic002.cnblogs.com/ Images/2011/311362/2011071111465488.png "alt=" Android note <wbr>application object Usage-data transfer and memory leak issues "style=" margin:0 px;padding:0px;border:0px;width:686px; "/>

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

Snippets

1, passing data between two activity via application

650) this.width=650; "Title=" Android Notes <wbr>application object Usage-data transfer and memory leak issues "src=" http://pic002.cnblogs.com/ Images/2011/311362/2011071111461886.png "alt=" Android note <wbr>application object Usage-data transfer and memory leak issues "style=" margin:0 px;padding:0px;border:0px; "/> 650) this.width=650;" Title= "Android notes <wbr>application use of objects-data transfer and memory leak issues" Src= "Http://pic002.cnblogs.com/images/2011/311362/2011071111463294.png" alt= "Android notes <wbr> Use of application objects-data transfer and memory leak issues "style=" margin:0px;padding:0px;border:0px; "/>

Remember to remove the data stored in the application HashMap after the data transfer is complete in order to avoid a memory leak.

In fact, every Android application we develop is a appliction, and the definition of this class is often used in androidmanifes.xml. For example:

<  = "@drawable/icon"

     = "@string/app_name"

    android:name=".MyApplication">

This defines the properties of our entire application, such as name and icon.

We can customize Appliction, for example:

publicclass MyApplication extendsApplication {

}

This is the place where the default application we've been using for a long time is set to him as our own myapplication.
The purpose of the MyApplication class is to put some global and contextual variables and methods into use.


Apps for Android in application

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.