AppManager

1.统一应用程序中所有的Activity的栈管理
  涉及到activity的添加、删除指定、删除当前、删除所有、返回栈大小的方法

public class AppManager {

    private Stack<Activity> activityStack = new Stack<>();

    //设置当前类是单例的
private static AppManager appManager = new AppManager();
private AppManager(){}
public static AppManager getInstance(){
return appManager;
} public void addActivity(Activity activity){
activityStack.add(activity);
} public void removeActivity(Activity activity){
for(int i = activityStack.size() - 1; i >= 0 ; i--) {
if(activityStack.get(i).getClass().equals(activity.getClass())){
activity.finish();
activityStack.remove(i);
break;
}
}
} public void removeCurrent(){
// Activity activity = activityStack.get(activityStack.size() - 1);
Activity activity = activityStack.lastElement();
activity.finish();
activityStack.remove(activity);
} public void removeAll(){
for(int i = activityStack.size() - 1; i >= 0 ; i--) {
activityStack.get(i).finish();
activityStack.remove(i);
}
} public int size(){
return activityStack.size();
}
}

2.在MainActivity中,将当前的Activity添加到栈管理中:

protected void onCreate(Bundle savedInstanceState) {
//将当前的Activity添加到栈管理中
AppManager.getInstance().addActivity(this);
}

  

  

上一篇:浅析vue实例的生命周期(生命周期钩子)


下一篇:PLSQL Developer概念学习系列之登录连接Oracle时出现(没有登录) -PL / SQL Developer:ORA - 12541: TNS :无建听程序的错误解决办法(图文详解)