如何完全退出程序,在任意位置退出程序,如何管理當前的運行棧,知道activity的堆棧結構等,本文封裝了一個Activity管理類,能夠方便隨時退出程序。code
/** * 應用程序Activity管理類:用於Activity管理和應用程序退出 */ public class AppManager { private List<Activity> mActivityList = new LinkedList<Activity>(); private static AppManager instance; private AppManager(){} /** * 單一實例 */ public static AppManager getAppManager(){ if(instance==null){ instance=new AppManager(); } return instance; } /** * 添加Activity到堆棧 */ public void addActivity(Activity activity){ mActivityList.add(activity); } /** * 結束指定的Activity */ public void finishActivity(Activity activity){ if(activity!=null){ mActivityList.remove(activity); activity.finish(); activity=null; } } /** * 結束全部Activity */ public void finishAllActivity(){ while(mActivityList.size() > 0) { Activity activity = mActivityList.get(mActivityList.size() - 1); mActivityList.remove(mActivityList.size() - 1); activity.finish(); } } /** * 退出應用程序 */ public void AppExit() { Countly.sharedInstance().onStop(); try { finishAllActivity(); } catch (Exception e) { } } }
文章爲原創,轉載請註明出處。rem