Android 自定义Activity栈对Activity统一管理

转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6307239.html

public class AppManager {
  private static Stack<Activity> mStack;
  private static AppManager instance;

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

  /**
  * 添加Activi到堆栈
  * @param activity
  */
  public void addActivity(Activity activity){
    if(mStack!=null){
      mStack=new Stack<Activity>();
    }
    mStack.add(activity);
  }

  /**
  * 获取当前Activity(堆栈中最后压入的一个)
  * @return
  */
  public Activity currentActivity(){
    return mStack.lastElement();
  }

  public boolean isLastActivity(Class cls){
    if(mStack==null||mStack.size()==0){
      return false;
    }
    return mStack.get(mStack.size()-1).getClass().equals(cls);
  }

  /**
  * 结束当前Activity(堆栈中最后压入的一个)
  */
  public void finishCurrentActivity(){
    finishActivity(mStack.lastElement());
  }

  /**
  * 结束指定Acitivity
  * @param activity
  */
  public void finishActivity(Activity activity){
    if(activity!=null){
      mStack.remove(activity);
      activity.finish();
      activity=null;
    }
  }

  /**
  * 结束指定类名的Activity
  * @param cls
  */
  public synchronized void finishActivity(Class<?> cls){
    if (mStack==null||mStack.size()==0){
    return;
    }
    for (Activity activity:mStack) {
      if (activity.getClass().equals(cls)){
        finishActivity(activity);
      }
    }
  }

  /**
  * 结束所有Activity
  */
  public void finishAllActivity(){
    for (int i=0,size=mStack.size();i<size;i++){
      if(null!=mStack.get(i)){
        mStack.get(i).finish();
      }
    }
    mStack.clear();
  }

  /**
  * 退出应用程序
  * @param context
  */
  public void AppExit(Context context){
    finishAllActivity();
    System.exit(0);
  }
}

上一篇:基于HTML5和JSP实现的图片Ajax上传和预览


下一篇:一道背包神题-Petrozavodsk Winter-2018. Carnegie Mellon U Contest Problem I