What is application
Application and Activity,service are a system component of the Android framework, and when the Android program starts, 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 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.
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 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.
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)
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 reference
Static 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.
Snippets
1, passing data between two activity via application
Remember to remove the data stored in the application HashMap after the data transfer is complete in order to avoid a memory leak.
Transfer from http://blog.csdn.net/lieren666/article/details/7598288
Application Configuring the Global Context
The first step, write a global singleton pattern of the MyApplication inherits from the application overlay OnCreate, in this method is instantiated application
Second step, configure the global context
<application android:name= "Com.appstore.service.MyApplication" ></application>
The third step, use, when used when using the name of the class to access the context
Entry points for Android programs
Android uses a Google Dalvik VM, which differs greatly from traditional Java VMs in that the entry point in Sun's Java system is main () as in the standard C language, and each Android program contains a application instance. There are multiple activity, Service, ContentProvider, or broadcast Receiver in a application instance. Because most of the applications contain activity so, said many netizens think is the activity of OnCreate, but you did not find that your project has a number of activity? You may not have seen an Android app without activity.
In fact, in android.app.Application this package oncreate is the real Android entry point, but most developers do not have to rewrite the class, his inheritance relationship such as:
Java.lang.Object
? Android.content.Context
? Android.content.ContextWrapper
? Android.app.Application
The Android.app.Application class contains 4 public methods
void onconfigurationchanged (Configuration newconfig)
void OnCreate ()//This is the real entry point.
void Onlowmemory ()
void Onterminate ()
So I hope everyone, remember that the real Android entry point is application main, you can see the androidmanifest.xml of the inclusion of the relationship is clear, not every application must have activity.
Application about global variables in Android
In Android programming, nouns such as application seem to become less common, and the activity, intent, provider, broadcast, and service are more familiar to everyone. But in fact, Android application also has its own use.
Open the manifest file and you will see a application configuration tag, which is about the use of application. What is the use of application? Take a look at how it is described in the SDK:
Base class for those who need to maintain global application state. You can provide your own implementation by specifying it name in your Androidmanifest.xml ' s < application> tag, whi CH would cause that class to being instantiated for your when the process for your application/package is created.
This means that application is used to hold global variables, and then it exists when the package is created. So when we need to create global variables, we do not need to create the static variables of public permissions as J2SE, but directly in the application. Just call the context's Getapplicationcontext or activity's Getapplication method to get a Application object, and then do the appropriate processing.
Because the small project involves a lot of documents, here on the paste code.
Application file:
Java code:
public class TestApplication extends application {
private int curindex;
public int Getcurindex () {
return curindex;
}
public void Setcurindex (int curindex) {
This.curindex = Curindex;
}
@Override
public void OnCreate () {
Super.oncreate ();
}
@Override
public void Onterminate () {
Super.onterminate ();
}
}
There is a curindex and setter getter method in application.
Operations on application in the first acitivty:
Java code:
TestApplication application = (testapplication) this.getapplication ();
LOG.I ("Data", "" +application.getcurindex ());
Application.setcurindex (5);
A second activity:
Java code:
TestApplication application = (testapplication) this.getapplication ();
LOG.I ("Data", "" +application.getcurindex ());
Application.setcurindex (6);
A third activity:
Java code
Final TestApplication application = (testapplication) this.getapplication ();
LOG.I ("Data", "" +application.getcurindex ());
In the process of operation, each time kill the corresponding activity, and then into the next activity.