1.新建EditActivity.class类
import java.util.LinkedList;
import java.util.List;
import android.app.Activity;
import android.app.Application;
public class ExitActivity extends Application {
private List<Activity> mList = new LinkedList<Activity>();
private static ExitActivity instance;
private ExitActivity() {
}
public synchronized static ExitActivity getInstance() {
if (null == instance) {
instance = new ExitActivity();
}
return instance;
}
// add Activity
public void addActivity(Activity activity) {
mList.add(activity);
}
public void exit() {
try {
for (Activity activity : mList) {
if (activity != null)
activity.finish();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// System.exit(0);
for (Activity activity : mList) {
if (activity != null)
activity.finish();
}
}
}
}
2.在需要一次性退出的Activity类里添加如下代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.write_dynamic);
ExitActivity.getInstance().addActivity(this);//在onCreate方法里调用
}
3.在需要一次性退出时,调用方法
ExitActivity.getInstance().exit();