public class MyActivityManager {
private Stack<Activity> activityStack;
private static MyActivityManager instance;
private MyActivityManager() {
}
public static MyActivityManager getActivityManager() {
if (instance == null) {
instance = new MyActivityManager();
}
return instance;
}
// 退出栈顶
public void removeActivityFromStack(Activity activity) {
try {
if (activity != null && activityStack != null && activityStack.contains(activity)) {
// activity.finish();
activityStack.remove(activity);
activity = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void popActivity(Activity activity){
if(activity != null){
if(activityStack != null && activityStack.contains(activity)) {
activityStack.remove(activity);
}
activity.finish();
activity = null ;
}
}
public void removeBeforeMessageActivity() {
if (activityStack != null && !activityStack.empty()) {
int size = activityStack.size();
for (int i = size - 1; i >= 0; i--) {
Activity ac = activityStack.elementAt(i);
if (ac != null && ac instanceof MessageDetailActivity) {
PLog.e("huanhuan", "进来销毁聊天详情");
popActivity(ac);
break;
}
}
}
}
public void popCurretentActivity(){
Activity activity = currentActivity();
if (activity == null) {
return ;
}
popActivity(activity);
}
// 获得当前栈顶
public Activity currentActivity() {
Activity activity = null;
if (activityStack != null && !activityStack.empty()) {
activity = (Activity) activityStack.lastElement();
}
return activity;
}
// 当前Activity推入栈中
public void pushActivity(Activity activity) {
if (activityStack == null) {
activityStack = new Stack<Activity>();
}
if(activity instanceof MessageDetailActivity){
removeBeforeMessageActivity();
}
activityStack.add(activity);
}
public boolean isMainActivityContains(){
boolean ret = false ;
if (activityStack != null && !activityStack.empty()) {
int size = activityStack.size();
for (int i = size - 1; i >= 0; i--) {
Activity ac = activityStack.elementAt(i);
if (ac != null && ac instanceof SsMainActivity) {
ret = true;
break;
}
}
}
return ret ;
}
// 退出所有Activity
public void popAllActivity() {
while (true) {
Activity activity = currentActivity();
if (activity == null) {
break;
}
popActivity(activity);
}
}
public void popAllActivityExceptMain() {
while (true) {
Activity activity = currentActivity();
if (activity == null || (activityStack.size() == 1 && activity instanceof SsMainActivity)) {
break;
} else {
if (!(activity instanceof SsMainActivity)) {
popActivity(activity);
}
}
}
}
}
Activity管理类
最新推荐文章于 2021-03-13 15:14:47 发布