MVP项目研究——AppManager融合Dagger2和androidEventbus管理Activity的生命周期


一、本类API

  • 前台activity的保存设置与获取

    • getCurrentActivity()获取
    • setCurrentActivity(Activity currentActivity)设置
  • Activity跳转

    • startActivity(Intent intent)
    • startActivity(Class activityClass)
  • Activity生命周期管理

    • addActivity(Activity activity)添加Activity到集合
    • getActivityList()返回一个存储所有未销毁的activity的集合
    • removeActivity(Activity activity)删除集合里的指定activity
    • removeActivity(int location)删除集合里的指定位置的activity
    • killActivity(Class

二、本类使用方法

本类主要是activity生命周期的管理,可以通过持有AppManager对象执行对应的方法,
也可以通过eventbus post 远程遥控执行对应的方法。

三源码:

package com.daqsoft.ybarms.base;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Message;
import android.support.design.widget.Snackbar;
import android.view.View;

import com.daqsoft.ybarms.utils.LogUtils;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;
import org.simple.eventbus.ThreadMode;

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

import javax.inject.Inject;
import javax.inject.Singleton;


/**
 * Created by yanbo on 2017/7/21.
 * 管理所有的Acticity和前台Activity
 * 1、可以通过本类对象执行方法
 * 2、可以通过eventbus post 事件远程遥控执行对应的方法。
 */
@Singleton
public final class AppManager {

    public List mActivityList;//Activity集合
    private Application mApplication;
    private Activity mCurrentActivity;//当前在前台的Activity
    /**
     * 加入androidevenbus
     */
    public static final String APPMANAGER_MESSAGE = "appmanager_message";
    public static final String IS_NOT_ADD_ACTIVITY_LIST = "is_not_add_activity_list";//true 为不需要加入到 Activity 容器进行统一管理,反之亦然
    public static final int START_ACTIVITY = 0;
    public static final int SHOW_SNACKBAR = 1;
    public static final int KILL_ALL = 2;
    public static final int APP_EXIT = 3;


    @Inject
    public AppManager(Application application) {
        this.mApplication = application;
        EventBus.getDefault().register(this);
    }

    /**
     * 通过eventbus post事件,远程遥控执行对应方法
     */
    @Subscriber(tag = APPMANAGER_MESSAGE, mode = ThreadMode.MAIN)
    public void onReceive(Message message) {
        switch (message.what) {
            case START_ACTIVITY:
                if (message.obj == null)
                    break;
                dispatchStart(message);
                break;
            case SHOW_SNACKBAR:
                if (message.obj == null)
                    break;
                showSnackbar((String) message.obj, message.arg1 == 0 ? false : true);
                break;
            case KILL_ALL:
                killAll();
                break;
            case APP_EXIT:
                appExit();
                break;
            default:
                LogUtils.w("The message.what not match");
                break;
        }
    }


    private void dispatchStart(Message message) {
        if (message.obj instanceof Intent)
            startActivity((Intent) message.obj);
        else if (message.obj instanceof Class)
            startActivity((Class) message.obj);
    }

    /**
     * --------------------------------------showSnackbar显示消息
     *
     * @param message
     * @param isLong
     */
    public void showSnackbar(String message, boolean isLong) {
        if (getCurrentActivity() == null) {
            LogUtils.w("mCurrentActivity == null when showSnackbar(String,boolean)");
            return;
        }
        View view = getCurrentActivity().getWindow().getDecorView().findViewById(android.R.id.content);
        Snackbar.make(view, message, isLong ? Snackbar.LENGTH_LONG : Snackbar.LENGTH_SHORT).show();
    }

    /**
     * --------------------------------------前台activity的保存设置与获取
     * getCurrentActivity()获取
     * setCurrentActivity(Activity currentActivity)设置
     */
    public Activity getCurrentActivity() {
        return mCurrentActivity;
    }

    //保存前台Activity
    public void setCurrentActivity(Activity currentActivity) {
        this.mCurrentActivity = currentActivity;
    }

    /**
     * --------------------------------------Activity跳转
     * startActivity(Intent intent)
     * startActivity(Class activityClass)
     */
    //Intent方式
    public void startActivity(Intent intent) {
        if (getCurrentActivity() == null) {
            LogUtils.w("当使用Intent跳转的时候,当前Activity为null,我们使用new_task模式启动Activivity");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mApplication.startActivity(intent);
            return;
        }
        getCurrentActivity().startActivity(intent);
    }

    //Class方式
    public void startActivity(Class activityClass) {
        startActivity(new Intent(mApplication, activityClass));
    }


    /**
     * --------------------------------------Activity生命周期管理
     * addActivity(Activity activity)添加Activity到集合
     * getActivityList()返回一个存储所有未销毁的activity的集合
     * removeActivity(Activity activity)删除集合里的指定activity
     * removeActivity(int location)删除集合里的指定位置的activity
     * killActivity(Class activityClass)关闭指定activity
     * activityInstanceIsLive(Activity activity)指定的activity实例是否存活
     * activityClassIsLive(Class activityClass)指定的activity class是否存活(一个activity可能有多个实例)
     * killAll()关闭所有activity
     * appExit()退出应用程序
     * release()释放资源
     */
    public void addActivity(Activity activity) {
        if (mActivityList == null) {
            mActivityList = new LinkedList<>();
        }
        synchronized (AppManager.class) {
            if (!mActivityList.contains(activity)) {
                mActivityList.add(activity);
            }
        }
    }

    public List getActivityList() {
        if (mActivityList == null) {
            mActivityList = new LinkedList<>();
        }
        return mActivityList;
    }

    public void removeActivity(Activity activity) {
        if (mActivityList == null) {
            LogUtils.w("mActivityList == null when removeActivity(Activity)");
            return;
        }
        synchronized (AppManager.class) {
            if (mActivityList.contains(activity)) {
                mActivityList.remove(activity);
            }
        }
    }

    public Activity removeActivity(int location) {
        if (mActivityList == null) {
            LogUtils.w("mActivityList == null when removeActivity(int)");
            return null;
        }
        synchronized (AppManager.class) {
            if (location > 0 && location < mActivityList.size()) {
                return mActivityList.remove(location);
            }
        }
        return null;
    }

    public void killActivity(Class activityClass) {
        if (mActivityList == null) {
            LogUtils.w("mActivityList == null when killActivity");
            return;
        }
        for (Activity activity : mActivityList) {
            if (activity.getClass().equals(activityClass)) {
                activity.finish();
            }
        }
    }

    public boolean activityInstanceIsLive(Activity activity) {
        if (mActivityList == null) {
            LogUtils.w("mActivityList == null when activityInstanceIsLive");
            return false;
        }
        return mActivityList.contains(activity);
    }

    public boolean activityClassIsLive(Class activityClass) {
        if (mActivityList == null) {
            LogUtils.w("mActivityList == null when activityClassIsLive");
            return false;
        }
        for (Activity activity : mActivityList) {
            if (activity.getClass().equals(activityClass)) {
                return true;
            }
        }

        return false;
    }

    public void killAll() {
//        while (getActivityList().size() != 0) { //此方法只能兼容LinkedList
//            getActivityList().remove(0).finish();
//        }
        Iterator iterator = getActivityList().iterator();
        while (iterator.hasNext()) {
            Activity next = iterator.next();
            iterator.remove();
            next.finish();
        }
    }

    public void appExit() {
        try {
            killAll();
            if (mActivityList != null)
                mActivityList = null;
            ActivityManager activityMgr =
                    (ActivityManager) mApplication.getSystemService(Context.ACTIVITY_SERVICE);
            activityMgr.killBackgroundProcesses(mApplication.getPackageName());
            System.exit(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 释放资源
     */
    public void release() {
        EventBus.getDefault().unregister(this);
        mActivityList.clear();
        mActivityList = null;
        mCurrentActivity = null;
        mApplication = null;
    }
}

你可能感兴趣的:(【MVP项目研究】)