Android Application summary:

Source: Internet
Author: User

Android Application summary:
The life cycle of an Application is the longest. After a program is created, it is destroyed after it is started. We often save some global variables to be saved in the Application instead of the Activity.
It needs to be passed and will be destroyed as the Activity is destroyed. However, Application's strength has become its weakness, because the Application will not be destroyed in the process of the program, so it is likely to save too much data OOM, or
Memory leakage (if we save a reference to an Activity, the original Activity cannot be destroyed after it jumps to another Activity ).

We can create two hashmaps <String, Object> in the Application to transfer data (through sharing) and cache some data. However, if you need to cache a large amount of data, it is best to cache some SoftReference (soft reference) and cache the data to the local ROM or local SD card. If the cache in the application does not exist, search from the local cache. If the locally cached data does not exist
Obtained on the network. To avoid being referenced by another Activity, Application, or Runable
View TextView = new TextView (this) is held by other activities, so the Activity cannot be destroyed, resulting in Memory leakage.

If we want to hold some Activity References and do not want the Activity to be destroyed, we will save these activities to the List in the Application.

Public synchronized static void register (Activity activity) {if (activityList! = Null) {for (int I = 0; I <activityList. size (); I ++) {Activity ac = activityList. get (I); if (ac. getClass (). getName () = activity. getClass (). getName () {// Delete the old one and add a new activityList. remove (ac); if (! Ac. isFinishing () {ac. finish () ;}break ;}} activityList. add (activity) ;}} public synchronized static void unregister (Activity activity) {if (activityList! = Null & activityList. size ()! = 0) {for (int I = 0; I <activityList. size (); I ++) {Activity ac = activityList. get (I); if (ac. getClass (). getName () = activity. getClass (). getName () {activityList. remove (activity); if (! Ac. isFinishing () {ac. finish () ;}}} else {Log. I ("TAG", "No Activity in Pool ");}}


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.