手记

Android 常用开发工具类之 AppManager

Android 开发过程中总是需要对项目中的 activity 进行管理。如果单独处理每个 activity 的生命周期会显得很多余,也很麻烦。所以这时候就需要一个 activity 管理工具来统一管理项目中的 activity 。

常见写法


public class ActivityManagerTool {    private final List<Activity> activities = new LinkedList<Activity>();    private static ActivityManagerTool manager;    private boolean isExist = false;// activity 存在标志    public static Class<?> indexActivity; // 首页所在的activity所对应的类名,必须在打开首页设置此项    public static List<Class<?>> bottomActivities = new LinkedList<Class<?>>();// 底部导航类集合    /**     * 获得 activity管理对象     *     * @return     */    public static ActivityManagerTool getActivityManager() {        if (null == manager) {            manager = new ActivityManagerTool();        }        return manager;    }    /**     * 添加新的activity     *     * @param activity     * @return     */    public boolean add(final Activity activity) {        int position = 0;        // 判断是否自动清除非子activity//    if (AbsInitApplication.isUseActivityManager) {        // 导航栏activity进栈,删除非导航栏activity        if (isBottomActivity(activity)) {            for (int i = 0; i < activities.size() - 1; i++) {                if (!isBottomActivity(activities.get(i))) {
                    popActivity(activities.get(i));                    i--;                }                if (i > 0) {                    // 获得重复activity位置                    if (activities.get(i).getClass()
                            .equals(activity.getClass())) {                        isExist = true;                        position = i;                    }
                }
            }

        }//    }        if (!activities.add(activity)) {            return false;        }        // 删除重复activity        if (isExist) {            isExist = false;            activities.remove(position);        }        return true;    }    /**     * 关闭除参数activity外的所有activity     *     * @param activity     */    public void finish(final Activity activity) {        for (Activity iterable : activities) {            if (activity != iterable) {
                iterable.finish();            }
        }
    }    /**     * 关闭所有的activity     */    public void exit() {        for (Activity activity : activities) {            if (activity != null) {
                activity.finish();            }
        }        try {
            Thread.sleep(200);        } catch (InterruptedException e) {
            e.printStackTrace();        }
        System.exit(0);    }    /**     * 删除指定activity     *     * @param activity     */    private void popActivity(final Activity activity) {        if (activity != null) {
            activity.finish();            activities.remove(activity);        }

    }    /**     * 删除已经finish的activity     *     * @param sourceActivity     */    public void removeTemporaryActivities(final Class<Activity> targetclazz,                                          final Activity sourceActivity) {        if (targetclazz == null || sourceActivity == null) {            return;        }        int begin = -1;        int end = -1;        Activity activity;        for (int i = activities.size() - 1; i >= 0; i--) {
            activity = activities.get(i);            if (activity.getClass() == targetclazz && end == -1) {
                end = i;            }            if (sourceActivity == activity && begin == -1) {
                begin = i;            }            if (begin != -1 && end != -1) {                break;            }
        }        if (end != -1 && begin > end) {            for (int i = begin; i > end; i--) {
                activity = activities.get(i);                popActivity(activity);            }
        }
    }    /**     * 获得当前activity     *     * @return     */    @SuppressWarnings("unused")    public Activity currentActivity() {        if (activities == null || activities.isEmpty()) {            return null;        }

        Activity activity = activities.get(activities.size() - 1);        return activity;    }    /*    * 是否存在指定的Activity    * */    public boolean isExistActivity(final Class<? extends Activity> targetclazz) {
        Activity activity;        if (activities == null) return false;        for (int i = activities.size() - 1; i >= 0; i--) {
            activity = activities.get(i);            if (activity.getClass() == targetclazz) {                return true;            }
        }        return false;    }    public boolean isTopActivity(final Activity activity) {

        Activity currentActivity = currentActivity();        if (currentActivity == null) return false;        return currentActivity.equals(activity);    }    /**     * activity是否为底部导航     *     * @return     */    public boolean isBottomActivity(final Activity activity) {        for (int i = 0; i < bottomActivities.size(); i++) {            if (activity.getClass() == bottomActivities.get(i)) {                return true;            }
        }        return false;    }    /**     * 如需返回IndexActivity则返回IndexActivity     *     * @param context     */    public void backIndex(final Context context) {        if (activities.size() <= 0) {            return;        }        for (int i = activities.size() - 1; i >= 0; i--) {
            Activity activity = activities.get(i);            if (isBottomActivity(activity)) {
                Intent intent = new Intent();                intent.setClass(context, indexActivity);                context.startActivity(intent);            } else {
                popActivity(activity);            }
        }
    }    /**     * 如需返回IndexActivity则返回IndexActivity     *     * @param clazz     */    public <E extends Activity> boolean backToActivity(final Class<E> clazz) {        boolean flag = false;        if (activities.size() <= 0) {            return flag;        }        for (int i = activities.size() - 1; i >= 0; i--) {
            Activity activity = activities.get(i);            if (activity.getClass() == clazz) {
                flag = true;                break;            }
        }        if (flag) {            for (int i = activities.size() - 1; i >= 0; i--) {
                Activity activity = activities.get(i);                if (activity.getClass() != clazz) {
                    popActivity(activity);                } else {                    break;                }
            }
        }        return flag;    }    /**     * 删除已经finish的activity     *     * @param activity     */    public void removeActivity(final Activity activity) {        if (activity != null) {            activities.remove(activity);        }
    }    /**     * 初始化,存储底部导航类     *     * @param activityClass     */    public void setBottomActivities(final Class<?> activityClass) {        if (activityClass != null) {            bottomActivities.add(activityClass);        }
    }    public List<Activity> getActivities() {        return activities;    }
}

  1. public class AppManager {  

  2.   

  3.     private static Stack<Activity> activityStack;  

  4.     private static AppManager instance;  

  5.   

  6.     private AppManager(){  

  7.   

  8.     }  

  9.     /**  

  10.      * 单一实例  

  11.      */  

  12.     public static AppManager getAppManager(){  

  13.         if(instance==null){  

  14.             instance=new AppManager();  

  15.         }  

  16.         return instance;  

  17.     }  

  18.     /**  

  19.      * 添加Activity到堆栈  

  20.      */  

  21.     public void addActivity(Activity activity){  

  22.         if(activityStack==null){  

  23.             activityStack=new Stack<Activity>();  

  24.         }  

  25.         activityStack.add(activity);  

  26.     }  

  27.     /**  

  28.      * 获取当前Activity(堆栈中最后一个压入的)  

  29.      */  

  30.     public Activity currentActivity(){  

  31.         if(activityStack==null)  

  32.             return null;  

  33.         Activity activity=activityStack.lastElement();  

  34.         return activity;  

  35.     }  

  36.     /**  

  37.      * 结束当前Activity(堆栈中最后一个压入的)  

  38.      */  

  39.     public void finishActivity(){  

  40.         try{  

  41.             if(activityStack==null)  

  42.                 return;  

  43.             Activity activity=activityStack.lastElement();  

  44.             finishActivity(activity);  

  45.         }catch(Exception e){  

  46.             e.printStackTrace();  

  47.         }  

  48.     }  

  49.     /**  

  50.      * 结束指定的Activity  

  51.      */  

  52.     public void finishActivity(Activity activity){  

  53.         if(activity!=null){  

  54.             activityStack.remove(activity);  

  55.             activity.finish();  

  56.             activity=null;  

  57.         }  

  58.     }  

  59.     /**  

  60.      * 结束指定类名的Activity  

  61.      */  

  62.     public void finishActivity(Class<?> cls){  

  63.         for (Activity activity : activityStack) {  

  64.             if(activity.getClass().equals(cls) ){  

  65.                 finishActivity(activity);  

  66.             }  

  67.         }  

  68.     }  

  69.     /**  

  70.      * 结束指定的多个Activity  

  71.      */  

  72.     public void finishActivityList(List<Class<?>> clsList){  

  73.         for(int j = 0; j < clsList.size(); j++){  

  74.             Class<?> cls = clsList.get(j);  

  75.             for (int i = 0, size = activityStack.size(); i < size; i++){  

  76.                 if (activityStack.get(i).getClass().equals(cls)){  

  77.                     activityStack.get(i).finish();  

  78.                 }  

  79.             }  

  80.         }  

  81.     }  

  82.     /**  

  83.      * 结束所有Activity  

  84.      */  

  85.     public void finishAllActivity(){  

  86.         for (int i = 0, size = activityStack.size(); i < size; i++){  

  87.             if (null != activityStack.get(i)){  

  88.                 activityStack.get(i).finish();  

  89.             }  

  90.         }  

  91.         activityStack.clear();  

  92.     }  

  93.   

  94.   

  95.     /**  

  96.      * 退出应用程序  

  97.      */  

  98.     public void AppExit(Context context) {  

  99.         try {  

  100.             finishAllActivity();  

  101.             //退出程序  

  102.             android.os.Process.killProcess(android.os.Process.myPid());  

  103.             System.exit(1);  

  104.         } catch (Exception e) {  

  105.         }  

  106.     }  

  107.   

  108. }  

在基类里调用,这样我们就不需要单独在某个Activity里面添加重复逻辑。


protected void onCreate(@Nullable Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    ActivityManagerTool.getActivityManager().add(this);}
@Overrideprotected void onDestroy() {    super.onDestroy();    DialogManager.hideDialog();    ActivityManagerTool.getActivityManager().removeActivity(this);    EventBus.getDefault().unregister(BaseActivity.this);}

原文出处

0人推荐
随时随地看视频
慕课网APP