activity 跳转后finish由于动画出现黑屏的补救

在跳转之前。将父activity 添加到appManager中,不直接执行finish,在子activity 中合适的位置。比如:

getWindow().getDecorView().post(new Runnable() {
 //TODO 移除父activity
});

或者finish子acvitity的时候复写 

@Override
public void finish() {
 //TODO 移除父activity
   super.finish();
}

调用AppManager.finishActivityFromName(activity.class.getSimpleName())移除父activity再调用自身finish()方法,搞定。


/** 应用程序Activity管理类 */
public class AppManager {
   private static Stack<Activity> activityStack;
   private static AppManager instance;

   private AppManager() {
   }

   /**
    * 单一实例
    */
   public static AppManager getAppManager() {
      if (instance == null) {
         instance = new AppManager();
      }
      return instance;
   }

   /**
    * 添加Activity到堆栈
    */
   public void addActivity(Activity activity) {
      if (activityStack == null) {
         activityStack = new Stack<Activity>();
      }
      activityStack.add(activity);
   }

   /**
    * 获取当前Activity(堆栈中最后一个压入的)
    */
   public Activity currentActivity() {
      Activity activity = activityStack.lastElement();
      return activity;
   }

   /**
    * 结束当前Activity(堆栈中最后一个压入的)
    */
   public void removeActivity() {
      Activity activity = activityStack.lastElement();
      removeActivity(activity);
   }

   /**
    * 结束指定的Activity
    */
   public void removeActivity(Activity activity) {   
      if (activity != null) {
         activityStack.remove(activity);
         activity.finish();
         activity = null;
      }
   }

   /**
    * 结束指定类名的Activity
    */
   public void finishActivity(Class cls) {
      for (Activity activity : activityStack) {
         if (activity.getClass().equals(cls)) {
            if (activity != null) {
               activity.finish();
               activity = null;
            }
         }
      }
   }
   
   /**
    * 判断指定类名的activity是否在任务中
    */
   public boolean containActivity(Class cls) {
        for (Activity activity : activityStack) {
            if (activity.getClass().equals(cls)) {
                if (activity != null) {
                   return true;
                }
            }
        }
        return false;
    }
   /**
    * 结束所有Activity
    */
   public void finishAllActivity() {
      if (activityStack==null) {
         return;
      }
      for (int i = 0, size = activityStack.size(); i < size; i++) {
         Activity activity = activityStack.get(i);
          if (null != activity) {
              activity.finish();
              activity = null;
         }
      }
      activityStack.clear();
   }

   /**
    * 移除指定名字的activity
    */
   public void removeActivityFromName(String activityName) {
      if (activityStack==null || TextUtils.isEmpty(activityName)) {
         return;
      }
      for (int i = 0; i < activityStack.size(); i++) {
         if (activityName.equals(activityStack.get(i).getClass().getSimpleName())){
            activityStack.get(i).finish();
         }
      }
   }
   /**
    * 移除除了指定activity的所有activity
    */
   public void finishAllExcept(String activityName) {
      if (activityStack==null || TextUtils.isEmpty(activityName)) {
         return;
      }
      for (int i = 0; i < activityStack.size(); i++) {
         if (activityName.equals(activityStack.get(i).getClass().getSimpleName())){

         }else{
            activityStack.get(i).finish();
         }
      }
   }
}

你可能感兴趣的:(android,流水账)