Activity管理

/**
* 所有页面跳转总控制
* Created by Kkan on 2016/12/18.
*/
public class ActivityJumpControl {
@SuppressLint(“StaticFieldLeak”)
private static ActivityJumpControl mInstance = null;
private Activity mOwner = null;

private ActivityJumpControl(Activity activity) {
    mOwner = activity;
}

/**
 * 获取页面跳转控制器
 *
 * @param activity 当前页面
 * @return 页面跳转控制器
 */
public static ActivityJumpControl getInstance(Activity activity) {
    if (mInstance == null) {
        mInstance = new ActivityJumpControl(activity);
    } else {
        mInstance.mOwner = activity;
    }
    return mInstance;
}

/**
 * 存放activity的列表
 */
private static HashMap, Activity> activities = new LinkedHashMap<>();

/**
 * 向控制器中添加activity
 *
 * @param activity Activity
 * @param clz      class
 */
public static void addActivity(Activity activity, Class clz) {
    activities.put(clz, activity);
}

/**
 * 判断一个Activity 是否存在
 *
 * @param clz class
 * @return boolean true 存在 false不存在
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static  boolean isActivityExist(Class clz) {
    boolean res;
    Activity activity = getActivity(clz);
    res = activity != null && !(activity.isFinishing() || activity.isDestroyed());

    return res;
}

/**
 * 获得指定activity实例
 *
 * @param clazz Activity 的类对象
 * @return Activity
 */
public static  T getActivity(Class clazz) {
    return (T) activities.get(clazz);
}

/**
 * 移除activity,代替finish
 *
 * @param activity Activity
 */
public static void removeActivity(Activity activity) {
    if (activities.containsValue(activity)) {
        activities.remove(activity.getClass());
    }
}

/**
 * 移除所有的Activity
 */
public static void removeAllActivity() {
    if (activities != null && activities.size() > 0) {
        Set, Activity>> sets = activities.entrySet();
        for (Map.Entry, Activity> s : sets) {
            if (!s.getValue().isFinishing()) {
                s.getValue().finish();
            }
        }
    }
    assert activities != null;
    activities.clear();
}

}

“`

你可能感兴趣的:(安卓工具类)