Activity想关哪个轻松关哪个

关于

好久没有更新文章了,感觉该写点什么了。就简单点的啦,这次分享一个Activity管理工具,轻松关闭想关闭的页面。写这个的原因也是因为,在当前页面需要关闭上一个页面或其他之前打开的页面。其实网上也有很多的办法,比如static修饰,比如Application、队列管理等等,感比较麻烦,不注意容易内存泄漏。所以自己搞了一个管理类,只需要在Application中的onCreate方法中初始化一次,就OK了,剩下的就是你要关哪个页面了。

演示

还是先来个GIF图演示一下吧

操作

  • 初始化

自定义Application,在onCreate中添加XActivityManager.addActivityListener(this);

public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        XActivityManager.addActivityListener(this);
    }
}
  • 怎么用?
    假如说当前有Activity:MainActivity、OneActivity、TwoActivity、ThreeActivity、FourActivity、FiveActivity
  1. 当你在ThreeActivity关闭TwoActivity
XActivityManager.getInstance().finishActivityByName(TwoActivity.class);
  1. 当你在FourActivity想关闭所有Activity排除MainActivity
XActivityManager.getInstance().finishAllExceptThisActivity(MainActivity.class);

实现思路

代码在下面大家可以先看一看

  1. 代码的主要思路在于添加了关于Application对Activity的监听,当监听到有Activity运行是则添加到Map中,如果Activity销毁则从Map中删除。
  2. 用Map管理,是方便与通过名字就可以finish掉Activity。
  3. 由于可能造成内存泄漏,Map添加的是含有Activity的WeakReference。
  4. 通过方法传入Activity的class,然后获取Activity名字,通过Map的key得到Activity。

代码

import android.app.Activity;
import android.app.Application;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;

public class XActivityManager {
    private static XActivityManager sInstance = new XActivityManager();
    private WeakReference<Activity> sCurrentActivityWeakRef;
    private Map<String, WeakReference<Activity>> mAllActivity;


    private XActivityManager() {
        mAllActivity = new HashMap<>();
    }

    public static XActivityManager getInstance() {
        return sInstance;
    }

    /**
     * 获取当前Activity
     * @return
     */
    public Activity getCurrentActivity() {
        Activity currentActivity = null;
        if (sCurrentActivityWeakRef != null) {
            currentActivity = sCurrentActivityWeakRef.get();
        }
        return currentActivity;
    }

    private void setCurrentActivity(Activity activity) {
        sCurrentActivityWeakRef = new WeakReference<>(activity);
        mAllActivity.put(activity.getClass().getSimpleName(), sCurrentActivityWeakRef);
        for (Map.Entry<String, WeakReference<Activity>> entry :
                mAllActivity.entrySet()) {
            WeakReference<Activity> wr = entry.getValue();
            if (wr.get() == null) continue;
            Log.i("XActivityManager", "当前活动Activity:" + wr.get().getClass().getSimpleName());
        }

        Log.i("XActivityManager", "---------------------------------------");
    }

    private void removeThisActivity(Activity activity) {
        mAllActivity.remove(activity.getClass().getSimpleName());
    }

    /**
     * 通过Activity 名字干掉这个Activity
     *
     * @param activityClass
     */
    public void finishActivityByName(Class<? extends Activity> activityClass) {
        WeakReference<Activity> wr = mAllActivity.get(activityClass.getSimpleName());
        if (wr == null) return;
        Activity activity = wr.get();
        if (
                activity == null
                || activity.isFinishing()
                || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && activity.isDestroyed())
                )
            return;
        activity.finish();
    }

    /**
     * 干掉所有的activity排除这个Activity
     */
    public void finishAllExceptThisActivity(Class<? extends Activity> activityClass) {
        for (Map.Entry<String, WeakReference<Activity>> entry :
                mAllActivity.entrySet()) {
            WeakReference<Activity> wr = entry.getValue();
            if (
                    wr.get() == null
                    || wr.get().isFinishing()
                    || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && wr.get().isDestroyed())
                    || entry.getKey().equals(activityClass.getSimpleName())
                    )
                continue;
            wr.get().finish();
        }
    }

    /**
     * 添加对Activity的监听
     */
    public static void addActivityListener(Application app) {

        app.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
            }

            @Override
            public void onActivityStarted(Activity activity) {

            }

            @Override
            public void onActivityResumed(Activity activity) {
                XActivityManager.getInstance().setCurrentActivity(activity);
            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {
                XActivityManager.getInstance().removeThisActivity(activity);
            }
        });
    }

}

END

推荐阅读更多精彩内容