1. 程式人生 > >activity 跳轉後finish由於動畫出現黑屏的補救

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(); } } } }