-android Custom application manage the life cycle of your activity

Source: Internet
Author: User

To safely exit multiple created activity?

You can customize the application:myapplication.

Add a list member to save, some key created activity instances:

1 PrivateList<activity> activitylist =NewLinkedlist<activity>();2 3 //Save this for all activity4      Public voidaddactivity (activity activity) {5 Activitylist.add (activity);6     }7 8  Public voidexit () {9         //Save cache data to DBTenCacheManager CacheManager = cachemanager.getinstance ( This); One Cachemanager.savenewcache1todb (); A          This. Msgnewlistmap.clear (); -          for(Activity activity:activitylist) { - activity.finish (); the         } -}

Then, when Activit is created, the activity is added to the list in OnCreate

1 myapplication.getinstance (). addactivity (chatlistactivity.  this);

When the program exits, for example, double-click the return key to exit the program, call the application exit method:

1 //Exit2 @Override3      Public BooleanOnKeyDown (intKeyCode, KeyEvent event) {4         //TODO auto-generated Method Stub5         if(KeyCode = =keyevent.keycode_back) {6             LongCurrentTime =System.currenttimemillis ();7             if((Currenttime-touchtime) >=waitTime) {8 Showcustomtoast (r.string.main_exit);9Touchtime =currenttime;Ten}Else { One                 //android.os.Process.killProcess (Android.os.Process.myPid ()); A myapplication.getinstance (). exit (); -             } -             return true; the  -         } -         return Super. OnKeyDown (KeyCode, event); -}

Code full path: http://git.oschina.net/zj2012zy/Android-Demo/tree/master/CustomApplication

-android Custom application manage the life cycle of your activity

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.