在项目中我们经常要去管理activity,在退出app时有时不能退出所有的activity,这时我们需要自己定义一个用来管理activity的类。
package com.example.all.utils;
import java.util.Stack;
import android.app.Activity;
import android.util.Log;
public class ActivityManager {
private static ActivityManager instance;
private Stack activityStack;//activity栈
private ActivityManager() {
}
//单例模式
public static ActivityManager getInstance() {
if (instance == null) {
instance = new ActivityManager();
}
return instance;
}
//把一个activity压入栈中
public void pushOneActivity(Activity actvity) {
if (activityStack == null) {
activityStack = new Stack();
}
activityStack.add(actvity);
Log.d(“MyActivityManager “, “size = ” + activityStack.size());
Log.d(“MyActivityManager “, “size = ” + actvity.getClass());
}
//获取栈顶的activity,先进后出原则
public Activity getLastActivity() {
return activityStack.lastElement();
}
//移除一个activity
public void popOneActivity(Activity activity) {
if (activityStack != null && activityStack.size() > 0) {
if (activity != null) {
activity.finish();
activityStack.remove(activity);
activity = null;
}
}
}
//退出所有activity
public void finishAllActivity() {
if (activityStack != null) {
while (activityStack.size() > 0) {
Activity activity = getLastActivity();
if (activity == null) break;
popOneActivity(activity);
}
}
}
}