当我们需要退出,或者某些原因需要关闭所有的 Activty,这里一个简单的类可以实现:
package com.layout;
import java.util.LinkedList;
import java.util.List;
import android.app.Activity;
import android.app.Application;
/**
* 结束所有后台activity
*/
public class SysApplication extends Application {
//运用list来保存们每一个activity是关键
private List<Activity> mList = new LinkedList<Activity>();
//为了实现每次使用该类时不创建新的对象而创建的静态对象
private static SysApplication instance;
//构造方法
private SysApplication(){}
//实例化一次
public synchronized static SysApplication getInstance(){
if (null == instance) {
instance = new SysApplication();
}
return instance;
}
// add Activity
public void addActivity(Activity activity) {
mList.add(activity);
}
//关闭每一个list内的activity
public void exit() {
try {
for (Activity activity:mList) {
if (activity != null)
activity.finish();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
System.exit(0);
}
}
//杀进程
public void onLowMemory() {
super.onLowMemory();
System.gc();
}
}
-END