单例模式之强大的activity的管理

public class ActivityManager {
    //增加删除操作多,栈是先进后出
    public Stack<Activity> mActivities;

    private ActivityManager() {
        mActivities = new Stack<>();
    }

    //双重校验锁
    private static volatile ActivityManager mInstance;

    public static ActivityManager getInstance() {
        if (mInstance == null) {
            synchronized (ActivityManager.class) {
                if (mInstance == null) {
                    mInstance = new ActivityManager();
                }
            }
        }
        return mInstance;
    }

    /**
     * 添加
     */
    public void attach(Activity activity) {
        if (activity != null) {
            mActivities.add(activity);
        }
    }

    /**
     * 移除解绑 - 防止内存泄漏
     */
    public void detach(Activity activity) {
        int size = mActivities.size();
        for (int i = size - 1; i >= 0; i--) {

            Activity currentActivity = mActivities.get(i);
            if (currentActivity.getClass().equals(activity.getClass())) {
                mActivities.remove(i);
                size--;
                i++;
            }
        }
    }

    /**
     * 移除指定的activity
     * 先进后出
     */
    public void remove(Activity activity) {
        int size = mActivities.size();
        for (int i = size - 1; i > 0; i--) {
            Activity currentActivity = mActivities.get(i);
            if (currentActivity.getClass().equals(activity.getClass())) {
                //销毁当前activity
                currentActivity.finish();
                mActivities.remove(i);//从栈空间移除
                i++;
                size--;
            }
        }
    }
    /**
     * 根据当前的类名进行关闭
     */
    public void finish(Class<? extends Activity> activityClass){
        int size = mActivities.size();
        for (int i = size-1; i > 0; i--) {
            Activity activity = mActivities.get(i);
            if(activity.getClass().getCanonicalName().equals(activityClass.getCanonicalName())){
                activity.finish();
                mActivities.remove(i);
                i++;
                size--;
            }
        }
    }
    /**
     * 删除当前的activity
     */
    public void removeCurrent() {
     /*   Activity activity = mActivities.get(mActivities.size() - 1);
        activity.finish();
        mActivities.remove(mActivities.size()-1);*/
        Activity activity = mActivities.lastElement();
        activity.finish();
        mActivities.remove(activity);
    }

    /**
     * 移除所有的activity
     */
    public void removeAll() {
        for (int i = mActivities.size() - 1; i >= 0; i--) {
            Activity activity = mActivities.get(i);
            activity.finish();
            mActivities.remove(activity);
        }
    }

    //返回栈大小
    public int size() {
        return mActivities.size();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值