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