如何彻底退出程序,在任意位置退出程序,如何管理当前的运行栈,知道activity的堆栈结构等,本文封装了一个Activity管理类,可以方便随时退出程序。
/** * 应用程序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) { } } }