关闭一个或多个activity

//Activity与class获取类名的方式不同
if(act.getClass().getName().equals(activity.getName())){
index = activityList.indexOf(act);
break;
}
-------------------


package com.example.testand;


import java.util.LinkedList;
import java.util.List;


import android.app.Activity;
import android.app.Application;


public class CardActivitySkipUtil extends Application {


private List<Activity> activityList = new LinkedList<Activity>();
private static CardActivitySkipUtil instance;


private CardActivitySkipUtil() {
}


// 单例模式中获取唯一的MyApplication实例
public static CardActivitySkipUtil getInstance() {
if (null == instance) {
instance = new CardActivitySkipUtil();
}
return instance;
}


// 添加Activity到容器中
public void addActivity(Activity activity) {
activityList.add(activity);
}


// 遍历所有Activity并finish
public void exit() {
for (Activity activity : activityList) {
activity.finish();
}
}
// finish当前
public void exit(Activity activity) {
if(activityList != null && activityList.size() > 0)
{
int size = activityList.size();
activity.finish();
activityList.remove(size - 1);
}
}


/**
* finish多个activity,
* @param activity(关掉这个activity后边的多个类)
*/
public void finishMultiAct(Class<?> activity) {
if(activityList != null && activityList.size() > 0)
{
int size = activityList.size();
int start  =thisActIndex(activity);

if(start > -1){//activity存在
for (int i = start+1; i < size; i++) {//把后面的全部关掉
activityList.get(i).finish();
activityList.remove(size - 1);
}
}
}
}
/**
* 是否存在这个activity
* @param activity
* @return
*/
public boolean isHaveThisAct(Class<?> activity){
boolean isHave = false;
for (Activity act : activityList) {
if(act.getClass().getName().equals(activity.getName())){
isHave = true;
break;
}
}
return isHave;
}
/**
* 是否存在这个activity,并返回其所在index
* @param activity
* @return -1 不存在
*/
public int thisActIndex(Class<?> activity){
int index = -1;
for (Activity act : activityList) {
//Activity与class获取类名的方式不同
if(act.getClass().getName().equals(activity.getName())){
index = activityList.indexOf(act);
break;
}
}
return index;
}
/**
* finish多个activity,
* @param activity(关掉这个activity后边的多个类)
*/
public void finishMultiAct(Activity activity) {
if(activityList != null && activityList.size() > 0)
{
int size = activityList.size();
int start  =thisActIndex(activity);

if(start > -1){//activity存在
for (int i = start+1; i < size; i++) {//把后面的全部关掉
activity.finish();
activityList.remove(size - 1);
}
}
}
}
/**
* 是否存在这个activity
* @param activity
* @return
*/
public boolean isHaveThisAct(Activity activity){
for (Activity act : activityList) {
if(act.getClass().getName().equals(activity.getClass().getName())){
return true;
}
}
return false;
}


/**
* 是否存在这个activity,并返回其所在index
* @param activity
* @return -1 不存在
*/
public int thisActIndex(Activity activity){
int index = -1;
for (Activity act : activityList) {
if(act.getClass().getName().equals(activity.getClass().getName())){
index = activityList.indexOf(act);
break;
}
}
return index;
}

public List<Activity> getActivityList() {
return activityList;
}


public void setActivityList(List<Activity> activityList) {
this.activityList = activityList;
}


}
发布了6 篇原创文章 · 获赞 6 · 访问量 1万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览