Detailed introduction to the Android application class

Source: Internet
Author: User
<span id="Label3"></p><p><p><strong>first, take a look at what the document Says.</strong></p></p><p><p>Base class for those who need to maintain global application State. You can provide your own implementation by specifying it name in your</p></p><p><p>Androidmanifest.xml ' s <application> tag, which would cause that class-to is instantiated for your when the process for Your application/package is Created.</p></p><p><p>The application class is used to maintain the global state of the Application. You can provide your own implementation and indicate his name in the <application> tag of the Androidmanifest.xml file, which will result in the instantiation of your own application class when you create the Application.</p></p><p><p></p></p><p><p>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 class of singleton (Singleton) Mode. 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 globally unique, objects that are obtained in different activity,service are the same object. So through the application to do some: data transfer, data sharing, data caching and other Operations.</p></p><p><p></p></p><p><p><strong>second, rewrite the life-cycle events of the application class</strong></p></p><p><p>public <span style="background-color: #ffffff;">void <span class="sympad">onCreate<span class="normal">()</span> </span> </span></p></p><p><p><span class="sympad"><span class="normal">Called when the application is created, this method can be implemented to create and instantiate any application state variables or shared Resources. You can also get a single case of application in this Method.</span></span></p></p><p><p></p></p><p><p><span class="sympad"><span class="normal">public void <span class="sympad">onterminate <span class="normal">()</span> </span></span></span></p></p><p><p><span class="sympad"><span class="normal"><span class="sympad"><span class="normal">When the Application object is terminated, it is not guaranteed to be called, and when the program is terminated by the kernel to release resources for other applications, it will not be alerted and the process is terminated directly without invoking the Onterminate method of the Application's Object.</span></span></span></span></p></p><p><p></p></p><p><p><span class="sympad"><span class="normal">public void <span class="sympad">onlowmemory<span class="normal">()</span> </span></span></span></p></p><p><p><span class="sympad"><span class="normal"><span class="sympad"><span class="normal">When system resources are scarce, we can release additional memory here, which is usually done only when the background process has ended, but the foreground application is still missing memory when it is Called. You can override this method to empty the cache or release unnecessary resources.</span></span></span></span></p></p><p><p></p></p><p><p><span class="sympad"><span class="normal"><span class="sympad"><span class="normal">public void <span class="sympad">ontrimmemory<span class="normal">(int. level)</span> </span></span></span></span></span></p></p><p><p><span class="sympad"><span class="normal"><span class="sympad"><span class="normal"><span class="sympad"><span class="normal">Called when the runtime determines that the current application should reduce its memory overhead (usually when running into the background), including a level parameter that provides the context for the Request.</span></span></span></span></span></span></p></p><p><p></p></p><p><p><span class="sympad"><span class="normal"><span class="sympad"><span class="normal"><span class="sympad"><span class="normal">public void <span class="sympad">onconfigurationchanged <span class="normal">(Configuration newconfig)</span> </span></span></span></span></span></span></span></p></p><p><p><span class="sympad"><span class="normal"><span class="sympad"><span class="normal"><span class="sympad"><span class="normal"><span class="sympad"><span class="normal">Unlike Activity, when configuration changes, application objects are not terminated and Restarted. If the values used by the application depend on a specific configuration, override this method to load these values, or change the configuration values at the application Level.</span></span></span></span></span></span></span></span></p></p><p><p></p></p><p><p><strong><span class="sympad"><span class="normal"><span class="sympad"><span class="normal"><span class="sympad"><span class="normal"><span class="sympad"><span class="normal">Iii. Passing data through application</span></span></span></span></span></span></span></span></strong></p></p><p><p> <span class="sympad"><span class="normal"><span class="sympad"><span class="normal"><span class="sympad"><span class="normal"><span class="sympad"><span class="normal"> If there is an activity a, jump to activity B, and need to pass some data, the usual practice is Intent.putextra () let Intent carry, or have a bundle to add information to the bundle let Intent pass bundle object, Implement PASS. But this way </span> </span> </span> </span> </span> </span> </span> </span> <span class="sympad"><span class="normal"><span class="sympad"><span class="normal"><span class="sympad"><span class="normal"><span class="sympad"> <span class="normal" there is a problem that the data types intent and bundles can carry are basic types, if you want to implement complex transfer it cumbersome often requires implementation of serializable or parcellable interface. this actually kind android < span> </span> </span> </span> </span> </span> </span> </span> <span class="sympad"><span class="normal"> <span class="sympad"><span class="normal"><span class="sympad"><span class="normal"><span class="sympad"><span class="normal">IPC The method of data Transfer. 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. </span></span></span></span></span></span></span></span></span></p></p><p><p>the <span class="sympad"><span class="normal"><span class="sympad"><span class="normal"><span class="sympad"><span class="normal"><span class="sympad"><span class="normal">basic idea is to create a HashMap in application, with a string of key,object as value so that our HashMap can store any type of object. In activity a, put the object that needs to be passed in the HashMap,</span></span></span></span></span></span></span></span> <span class="sympad"><span class="normal"><span class="sympad"><span class="normal"><span class="sympad"><span class="normal"><span class="sympad"><span class="normal">and then pass the key to activity B through Intent or other means, activity B can take the object out of the HASHMAP based on the String. 。 As long as the downward transformation, the implementation of the object Delivery. </span></span></span></span></span></span></span></span> <span class="sympad"><span class="normal"><span class="sympad"><span class="normal"><span class="sympad"><span class="normal"><span class="sympad"><span class="normal">,</span></span></span></span></span></span></span></span></p></p><p><p></p></p><p><p><strong><span class="sympad"><span class="normal"><span class="sympad"><span class="normal"><span class="sympad"><span class="normal"><span class="sympad"><span class="normal">IV. Application Data Cache</span></span></span></span></span></span></span></span></strong></p></p><p><span class="sympad"><span class="sympad"><span class="normal"><span class="sympad"><span class="normal"><span class="sympad"><span class="normal"><span class="sympad"><span class="normal">I usually get used to building two HashMap in application one for data transfer (see three), one for caching some Data. For example, an activity needs to get some data from the website, and then we can save this data to</span></span></span></span></span></span></span></span></span><span class="sympad"><span class="sympad"><span class="normal"><span class="sympad"><span class="normal"><span class="sympad"><span class="normal"><span class="sympad"><span class="normal">Application</span></span></span></span></span></span></span></span></span><span class="sympad">the <span class="sympad"><span class="normal"><span class="sympad"><span class="normal"><span class="sympad"><span class="normal"><span class="sympad"><span class="normal">cached data can be used directly when the page jumps to other Activity and Returns. however, If you need to cache some large amounts of data, it is best to cache some (soft Reference) softreference, and to cache the data locally</span></span></span></span></span></span></span></span></span><span class="sympad"><span class="sympad"><span class="normal"><span class="sympad"><span class="normal"><span class="sympad"><span class="normal"><span class="sympad"><span class="normal">Rom, or Sdcard.</span></span></span></span></span></span></span></span></span><span class="sympad"><span class="sympad"><span class="normal"><span class="sympad"><span class="normal"><span class="sympad"><span class="normal"><span class="sympad"><span class="normal">. 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. </span></span></span></span></span></span></span></span></span></p><p><p></p></p><p><p><span class="sympad"><span class="normal"><span class="sympad"><span class="normal"><span class="sympad"><span class="normal"><span class="sympad"><span class="normal">  <span style="font-size: 16px; background-color: #ffffff;">Pretend to <strong>declare</strong> : This Bo article is not special note are original, if you need to reprint please keep the <span style="background-color: #ffffff;">original link</span> (http://www.cnblogs.com/kest/p/5137328.html) and author information <span style="background-color: #ffffff;"> K_est</span></span></span></span></span></span></span></span></span></span></p></p><p><p></p></p><p><p>Detailed introduction to the Android application class</p></p></span>

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.