如何 在多层acitivity中实现跳转并finish()之前所有的Activity


核心代码: ((MyApplication)getApplication()).getActivityManager().gotoHomeInquireActivity();
 

 

实现代码:自己写一个ActivitManager

public class ActivityManager {
 
 private ArrayList<Activity> mActivityArray = new ArrayList<Activity>();
 private Map<String, Integer> mActivityMap = new Hashtable<String, Integer>();
 
 public static void addActivity(ActivityManager actManager,String name, Activity act)
 {
  actManager.addActivity(name, act);
 }
 
 public void addActivity(String name, Activity act)
 {
  int nCount = mActivityArray.size();
  mActivityArray.add(act);
  mActivityMap.put(name, nCount);
 }
 
 public static void removeActivity(ActivityManager actManager,String name, Activity act)
 {
  actManager.removeActivity(name, act);
 }
 
 public void removeActivity(String name, Activity act) {
  Integer nIndex = mActivityMap.get(name);
  if (null == nIndex)
   return;
  int nCount = mActivityArray.size();
  if (nIndex >= nCount) {
   for (int i = nCount - 1; i >= 0; i--) {
    Activity activity = mActivityArray.get(i);
    if (activity == act) {
     mActivityMap.remove(name);
     mActivityArray.remove(act);
     break;
    }
   }
  } else {
   if (nIndex < nCount) {
    if (act == mActivityArray.get(nIndex)) {
     mActivityArray.remove(nIndex);
     mActivityMap.remove(name);
    } else {
     mActivityArray.remove(act);
    }
   } else {
    mActivityArray.remove(act);
   }
  }
 }
 
 public static void killMyProcess(ActivityManager actManager)
 {
  actManager.killMyProcess();
 }
 
 public void killMyProcess()
 {
  int nCount = mActivityArray.size();
        for (int i = nCount - 1; i >= 0; i--) {
         Activity activity = mActivityArray.get(i);
         activity.finish();
        }
        mActivityArray.clear();
        mActivityMap.clear();
  android.os.Process.killProcess(android.os.Process.myPid());
 }
 
 public void gotoHome1InquireActivity()
 {
  int nCount = mActivityArray.size();
        for (int i = nCount - 1; i >= 0; i--) {
         Activity activity = mActivityArray.get(i);
         if(! ( activity instanceof Home1InquireActivity ) ){
          activity.finish();
          mActivityArray.remove(i);
          if(mActivityMap.containsValue(i)){
           for (Map.Entry<String, Integer> entry : mActivityMap.entrySet()) {
                     if(entry.getValue() == i){
                      mActivityMap.remove(entry.getKey());
                      break;
                     }
                 }
          }
         }else{
          break;
         }
         
        }
       
 }
 
 public void gotoHome2InquireActivity()
 {
  int nCount = mActivityArray.size();
        for (int i = nCount - 1; i >= 0; i--) {
         Activity activity = mActivityArray.get(i);
         if(! ( activity instanceof Home2InquireActivity ) ){
          activity.finish();
          mActivityArray.remove(i);
          if(mActivityMap.containsValue(i)){
           for (Map.Entry<String, Integer> entry : mActivityMap.entrySet()) {
                     if(entry.getValue() == i){
                      mActivityMap.remove(entry.getKey());
                      break;
                     }
                 }
          }
         }else{
          break;
         }
         
        }
       
 }
}


如果进入一个Activity 就finish()上一个Activity,只要home那一页向新的Activity调转时,不要finish()homeActivity,android栈就会保存着那个home Activity, 所以在后面任意一个activity 不要startActivity(),直接finish(),就可以直接回到homeActivity.

 

另外一种高级点的方法就是上面讲的,相当于维护一个队列,拿着所有Activity的指引,删除所有的。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值