首先要利用单例模式做个继承application的全局容器,用来放置所有的activity
public class MyApplication extends Application { //对于新增和删除操作add和remove,LinedList比较占优势,因为ArrayList实现了基于动态数组的数据结构,要移动数据。LinkedList基于链表的数据结构,便于增加删除 private ListactivityList = new LinkedList (); private static MyApplication instance = null; //单例模式中获取唯一的MyApplication实例 public static MyApplication getInstance() { return instance; } //添加Activity到容器中 public void addActivity(Activity activity) { activityList.add(activity); } //遍历所有Activity并finish public void exit(){ for(Activity activity:activityList) { activity.finish(); } System.exit(0); } @Override public void onCreate() { super.onCreate(); context = getApplicationContext(); instance = this; }}
然后在每个activity中的onCreat方法中添加下面这句代码,是为了把当前的activity添加到容器中
MyApplication.getInstance().addActivity(this);
最后在想要结束的界面上添加下面这句代码 ,就可以直接关闭掉程序了
MyApplication.getInstance().exit();