实战项目中管理Activity的类,一般在BaseActivity会调用这个类,然后所有的Activity继承BaseActivity,这样管理好整个项目的Activity。
/**
* activity堆栈管理
*/
public class ActivityStackManager {
private static ActivityStackManager mInstance;
private static Stack<Activity> mActivityStack;
public static ActivityStackManager getInstance() {
if (null == mInstance) {
mInstance = new ActivityStackManager();
}
return mInstance;
}
private ActivityStackManager() {
mActivityStack = new Stack<Activity>();
}
/**
* 入栈
*
* @param activity
*/
public void addActivity(Activity activity) {
mActivityStack.push(activity);
}
/**
* 出栈
*
* @param activity
*/
public void removeActivity(Activity activity) {
mActivityStack.remove(activity);
}
/**
* 彻底退出
*/
public void finishAllActivity() {
Activity activity;
while (!mActivityStack.empty()) {
activity = mActivityStack.pop();
if (activity != null) {
activity.finish();
}
}
}
/**
* 结束指定类名的Activity
*
* @param cls
*/
public void finishActivity(Class<?> cls) {
for (Activity activity : mActivityStack) {
if (activity.getClass().equals(cls)) {
finishActivity(activity);
}
}
}
/**
* 查找栈中是否存在指定的activity
*
* @param cls
* @return
*/
public boolean checkActivity(Class<?> cls) {
for (Activity activity : mActivityStack) {
if (activity.getClass().equals(cls)) {
return true;
}
}
return false;
}
/**
* 结束指定的Activity
*
* @param activity
*/
public void finishActivity(Activity activity) {
if (activity != null) {
mActivityStack.remove(activity);
activity.finish();
activity = null;
}
}
/**
* finish指定的activity之上所有的activity
*
* @param actCls
* @param isIncludeSelf
* @return
*/
public boolean finishToActivity(Class<? extends Activity> actCls, boolean isIncludeSelf) {
List<Activity> buf = new ArrayList<Activity>();
int size = mActivityStack.size();
Activity activity = null;
for (int i = size - 1; i >= 0; i--) {
activity = mActivityStack.get(i);
if (activity.getClass().isAssignableFrom(actCls)) {
for (Activity a : buf) {
a.finish();
}
return true;
} else if (i == size - 1 && isIncludeSelf) {
buf.add(activity);
} else if (i != size - 1) {
buf.add(activity);
}
}
return false;
}}