Android——优雅落幕

单例封装杀死Activity

    • 代码
    • 引用
      • 添加Activity
      • 杀死所有Activity

代码

public class KillProcess  {
    private static KillProcess killProcess = null;
    private List activityList = new ArrayList<>(  );
    private KillProcess(){

    }
    public static  KillProcess getInstance(){
        if (killProcess == null){
            Sync();
        }
        return killProcess;
    }
    private static synchronized void Sync(){
        if (killProcess == null){
            killProcess = new KillProcess();
        }
    }
    public void addActivity(Activity activity) {
        activityList.add(activity);
    }
    public void finishAll() {
        for (Activity activity : activityList) {
            if (!activity.isFinishing()) {
                activity.finish();
            }
        }
    }
}

引用

获取实例

killProcess = KillProcess.getInstance();

添加Activity

在每一个Activity下添加如下代码

  killProcess.addActivity( AddClockActivity.this );

杀死所有Activity

 killProcess.finishAll();

你可能感兴趣的:(Android设计模式,android,java,apache,单例模式)