private static Stack<Activity> mActivityStack;
/**
* 添加Activity到堆栈
*/
public void addActivity(Activity activity) {
if (mActivityStack == null) {
mActivityStack = new Stack<Activity>();
}
mActivityStack.add(activity);
}
/**
* 获取栈顶Activity(堆栈中最后一个压入的)
*/
public Activity getTopActivity() {
Activity activity = mActivityStack.lastElement();
return activity;
}
/**
* 结束栈顶Activity(堆栈中最后一个压入的)
*/
public void killTopActivity() {
Activity activity = mActivityStack.lastElement();
killActivity(activity);
}
/**
* 结束指定的Activity
*/
public void killActivity(Activity activity) {
if (activity != null) {
mActivityStack.remove(activity);
activity.finish();
activity = null;
}
}
/**
* 结束指定类名的Activity
*/
public void killActivity(Class<?> cls) {
for (Activity activity : mActivityStack) {
if (activity.getClass().equals(cls)) {
killActivity(activity);
}
}
}
/**
* 结束所有Activity
*/
public void killAllActivity() {
if (Tools.isEmpty(mActivityStack)) {
return;
}
for (int i = 0, size = mActivityStack.size(); i < size; i++) {
if (null != mActivityStack.get(i)) {
mActivityStack.get(i).finish();
}
}
mActivityStack.clear();
}
网友评论