应用一键退出实现方法

当应用按退出键时,所有的界面都应该关闭。程序退出。实现思路:利用单例模式。
在每个activity中的oncreate中调用ExitApplication.getInstance().addActivity(this);,当要退出时,调用ExitApplication.getInstance().exit();

代码:
单例
package com.droi.smartlock.util;

import java.util.LinkedList;
import java.util.List;

import android.app.Activity;
import android.app.Application;

public class ExitApplication extends Application {

private List<Activity> activityList=new LinkedList<Activity>();

 private static ExitApplication instance;

 private ExitApplication()
 {
 }
public static ExitApplication getInstance()
{
if(null == instance)
{
 instance = new ExitApplication();
 }
 return instance;

 }
 //将activity加入到list中保存
 public void addActivity(Activity activity)
 {
 activityList.add(activity);
}
//依次将activity销毁
 public void exit()
 {

 for(Activity activity:activityList)
 {
 activity.finish();
 }

 System.exit(0);

 }

}

你可能感兴趣的:(应用一键退出实现方法)