likes
comments
collection
share

Android判断前后台的方法分析(二)

作者站长头像
站长
· 阅读数 67

( ´◔︎ ‸◔︎)🥬( ´◔︎ ‸◔︎)个人经验与分享,欢迎一起来探讨🥬( ´◔︎ ‸◔︎)🥬( ´◔︎ ‸◔︎)

判断前后台的方法往往五花八门,这次让我们再一起看一种Android中常用的前后台切换的方法 ActivityLifecycleCallbacks

ActivityLifecycleCallbacks是一个接口,它可以用来监听Activity的生命周期事件。当应用程序中的Activity经历生命周期事件时,系统会回调这个接口中的相应方法,从而允许开发者在Activity生命周期不同阶段插入自己的逻辑。

详细代码可以看ActivityonCreate方法dispatchActivityCreated,(其实看其他生命周期回调也是一样的)

protected void onCreate(@Nullable Bundle savedInstanceState) {
    ...不重要的代码省略了...
    dispatchActivityCreated(savedInstanceState);
    ...不重要的代码省略了...
}

再来往里看dispatchActivityCreated到底做了什么

private void dispatchActivityCreated(@Nullable Bundle savedInstanceState) {
    getApplication().dispatchActivityCreated(this, savedInstanceState);
    Object[] callbacks = collectActivityLifecycleCallbacks();
    if (callbacks != null) {
        for (int i = 0; i < callbacks.length; i++) {
            ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityCreated(this,
                    savedInstanceState);
        }
    }
}
private Object[] collectActivityLifecycleCallbacks() {
    Object[] callbacks = null;
    synchronized (mActivityLifecycleCallbacks) {
        if (mActivityLifecycleCallbacks.size() > 0) {
            callbacks = mActivityLifecycleCallbacks.toArray();
        }
    }
    return callbacks;
}

这里的mActivityLifecycleCallbacks就是我们注册进来的生命周期的监听回调

application.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks()

因此可以看出来,ActivityLifecycleCallbacks的核心逻辑是在应用程序的每一个Activity的生命周期内提供了一个挂钩(hook),允许开发者在特定的时机执行特定的操作,比如在Activity启动时记录日志、在Activity销毁时释放资源等。通过实现ActivityLifecycleCallbacks接口,开发者可以监听应用程序中所有Activity的生命周期事件,从而更好地管理和优化应用程序的性能和行为。

讲了这么多,来点干货,如果想封装一个Activity的状态管理类,并使其支持判断前后台,那么如下:

public final class ActivityStack {

    private static final LinkedList<Activity> sActivityStack = new LinkedList<>();

    private static Boolean isInBackground = true;

    private static WeakHashMap<Activity, String> sStartActivityWeakRef = new WeakHashMap<>();

    private static final CopyOnWriteArrayList<IActivityStackListener> sActivityStackListeners = new CopyOnWriteArrayList<>();


    public static void init(Application application) {
        application.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                synchronized (ActivityStack.class) {
                    sActivityStack.remove(activity);
                    sActivityStack.add(activity);
                    notifyActivityStackListener(activity, ActivityStackStatus.CREATE);
                }
            }

            @Override
            public void onActivityStarted(Activity activity) {
                synchronized (ActivityStack.class) {
                    sStartActivityWeakRef.put(activity, activity.getClass().getName());
                    notifyActivityStackListener(activity, ActivityStackStatus.START);
                }
            }

            @Override
            public void onActivityResumed(Activity activity) {
                synchronized (ActivityStack.class) {
                    if (isInBackground) {
                        isInBackground = false;
                    }
                    notifyActivityStackListener(activity, ActivityStackStatus.RESUME);
                }
            }

            @Override
            public void onActivityPaused(Activity activity) {
            }

            @Override
            public void onActivityStopped(Activity activity) {
                synchronized (ActivityStack.class) {
                    sStartActivityWeakRef.remove(activity);
                    if (!isInBackground && sStartActivityWeakRef.isEmpty()) {
                        isInBackground = true;
                    }
                    notifyActivityStackListener(activity, ActivityStackStatus.STOP);
                }
            }

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

            }

            @Override
            public void onActivityDestroyed(Activity activity) {
                sActivityStack.remove(activity);
                notifyActivityStackListener(activity, ActivityStackStatus.DESTROY);
            }
        });
    }

    public static synchronized Boolean getIsInBackground() {
        return isInBackground;
    }

    public static synchronized void addActivityStackListener(IActivityStackListener listener) {
        sActivityStackListeners.add(listener);
    }

    public static synchronized void removeActivityStackListener(IActivityStackListener listener) {
        sActivityStackListeners.remove(listener);
    }

    public static synchronized void notifyActivityStackListener(Activity activity, ActivityStackStatus stackStatus) {
        for (IActivityStackListener listener : sActivityStackListeners) {
            listener.onActivityStackChanged(stackStatus);
        }
    }
}
转载自:https://juejin.cn/post/7321596167585136676
评论
请登录