Android Use Application Summary

Source: Internet
Author: User

Description in SDK: The application class is for those basic classes that need to preserve the design of global variables, and you can implement your own in Androidmanifest.xml <application> tags. The result is that the class is created when your application or package is built.
Understanding: That is, application is used to preserve global variables, and is then present 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.

For example, in the Launcher module, it wrote a application itself and set it up in Androidmanifest.xml:

<application
Android:name= "Com.android.launcher2.LauncherApplication"

For his settings, refer to this module.

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 ()

What is 1:application?

1
Application, like Activity,service, is a system component of the Android framework that creates a application object to store some information about the system when the Android program starts. 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.

2: Passing Data through 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 index 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.

3:application data Cache

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.

6: Life cycle:

OnCreate When you create an application
Onterminate is called when the Application object is terminated, is not guaranteed to be called, when the program is terminated by the kernel to release resources for other applications, that
Will not be reminded, and the process is terminated directly without invoking the Onterminate method of the application's object
Onlowmemory This method is called when the daemon has terminated the resource and is scarce. A good application typically frees up some unnecessary
Resources to cope with situations where the daemon has terminated and the foreground application memory is not enough.
This method is triggered when the onconfigurationchanged configuration changes

Note: Application killed case analysis:
in order to decide which process to kill at low memory, Android divides the process into a "level of importance" based on the components and their state that are running within those processes. The importance of this is sorted by the following rules:
1: A front-end process can be a process that holds the activity that runs on the screen at the forefront and interacts with the user (the Onresume method is called), or it can hold a running Intentreceiver ( That is, he is performing his own onreceiveintent method) process. In the system, there will be only a few such processes, and unless the memory is low enough for these processes to run, the system will not actively kill those processes. At this point, the device usually has reached a state that requires memory grooming, so the process is killed in order not to let the user interface stop responding.
2: A visual process is a process that holds an activity that is visible to the user but does not appear in the front-end (when the OnPause method is called). For example, this process usually occurs in a front end activity that appears in a dialog box and keeps the previous activity visible. This process is considered extremely important by the system and is usually not killed unless the visible processes are killed in order to keep all the front-end processes running properly.
3: The service process is a process that holds a single service, which is initiated by the StartService () method, although the users of these processes are not directly visible, but usually they do work with a user who is very concerned (for example, playing mp3 in the background or downloading in the background) Upload files), so that the system does not kill the service process until all the front-end processes and visual processes are kept running properly.
4: A background process is a process that holds an activity that is no longer visible to the user (the OnStop () method is called). These processes do not directly affect the user experience. Joining these processes is complete, completing their lifecycle correctly (visit activity for more details), The system kills these background processes at any time when memory is freed for the first three processes. There are usually a lot of background processes running, so these processes are stored in an LRU list to ensure that the last process seen by the user is finally killed when the memory is low.
5: A null process is a process that does not hold any active application components. The only reason to keep this process is to provide a caching mechanism that shortens the startup time of his app's next run. As far as it is concerned, The purpose of the system to kill these processes is to balance the resources of the entire system between these empty processes and the underlying core caches.

Application file:

Java code:

 Public classTestApplicationextendsApplication {Private intCurindex; Public intGetcurindex () {returnCurindex;} Public voidSetcurindex (intCurindex) { This. Curindex =Curindex;} @Override Public voidonCreate () {Super. OnCreate ();} @Override Public voidonterminate () {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.

Android Use Application Summary

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.