使用ActivityLifecycleCallbacks监听前后台,获取当前Activity

一、ActivityLifecycleCallbacks接口介绍

API 14之后,在Application类中,提供了一个应用生命周期回调的注册方法,用来对应用的生命周期进行集中管理,这个接口叫registerActivityLifecycleCallbacks,可以通过它注册自己的ActivityLifeCycleCallback,每一个Activity的生命周期都会回调到这里的对应方法。

Activity生命周期图
使用ActivityLifecycleCallbacks监听前后台,获取当前Activity_第1张图片

ActivityLifecycleCallbacks接口代码

public interface ActivityLifecycleCallbacks {
    void onActivityCreated(Activity activity, Bundle savedInstanceState);
    void onActivityStarted(Activity activity);
    void onActivityResumed(Activity activity);
    void onActivityPaused(Activity activity);
    void onActivityStopped(Activity activity);
    void onActivitySaveInstanceState(Activity activity, Bundle outState);
    void onActivityDestroyed(Activity activity);
}

我们可以看到,两者是一一对应的,不管是做Activity的限制还是Activity的状态统计都是非常方便的,里面还有一个void onActivitySaveInstanceState(Activity activity, Bundle outState) 方法,非常方便我们来保存Activity状态数据。

二、ActivityLifecycleCallbacks 使用

package com.sunpeng.lifecycle;

import android.app.Application;

public class MainApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        // AppLifecycleCallback 实现 ActivityLifecycleCallbacks 接口方法
        this.registerActivityLifecycleCallbacks(new AppLifecycleCallback());
    }
}

三、Demo – 使用ActivityLifecycleCallbacks获取当前Activity

自定义一个ActivityLifecycleg管理类ActivityLifecycleMonitor

  1. 定义一个集合private final Set lifecycleCallbacks = new HashSet();用于外部类调用添加 ActivityLifecycleCallbacks对象

  2. 定义一个弱引用private WeakReference foregroundActivity = null;即当前Activity 对象

  3. new一个 ActivityLifecycleCallbacks - rootLifecycleCallback对象,在 onResume 和 onPause 里分别对 foregroundActivity 赋值,动态获取当前 Activity;
    在每个回调都遍历 lifecycleCallbacks 集合,为集合里的每个对象设置回调

private final ActivityLifecycleCallbacks rootLifecycleCallback = new ActivityLifecycleCallbacks() {

	public void onActivityResumed(Activity activity) {

            if (ActivityLifecycleMonitor.this.foregroundActivity != null) {
                ActivityLifecycleMonitor.this.foregroundActivity.clear();
                ActivityLifecycleMonitor.this.foregroundActivity = null;
            }

            ActivityLifecycleMonitor.this.foregroundActivity = new WeakReference(activity);

            Iterator var2 = ActivityLifecycleMonitor.this.lifecycleCallbacks.iterator();

            while (var2.hasNext()) {
                ActivityLifecycleCallbacks callback = (ActivityLifecycleCallbacks) var2.next();
                if (callback != null) {
                    try {
                        callback.onActivityResumed(activity);
                    } catch (Exception e) {
                    }
                }
            }

        }

        public void onActivityPaused(Activity activity) {

            if (ActivityLifecycleMonitor.this.foregroundActivity != null) {
                ActivityLifecycleMonitor.this.foregroundActivity.clear();
            }

            ActivityLifecycleMonitor.this.foregroundActivity = null;
            
            Iterator var2 = ActivityLifecycleMonitor.this.lifecycleCallbacks.iterator();

            while (var2.hasNext()) {
                ActivityLifecycleCallbacks callback = (ActivityLifecycleCallbacks) var2.next();
                if (callback != null) {
                    try {
                        callback.onActivityPaused(activity);
                    } catch (Exception e) {
                    }
                }
            }
        }
}
  1. 获取当前Activity
public Activity getForegroundActivity() {
        return this.foregroundActivity == null ? null : (Activity) this.foregroundActivity.get();
    }
  1. 注册 rootLifecycleCallback(需要在Applicition 里调用此方法)
public void registerRootLifeCycle(Context context) {
        try {
            Application application = null;
            if (context instanceof Application) {
                application = (Application) context;
            } else {
                Context applicationContext = context.getApplicationContext();
                if (applicationContext instanceof Application) {
                    application = (Application) applicationContext;
                }
            }

            if (application == null) {
                return;
            }

            application.registerActivityLifecycleCallbacks(this.rootLifecycleCallback);
        } catch (Exception var4) {
            var4.printStackTrace();
        }

    }
  1. lifecycleCallbacks 集合添加/删除对象(在其他类也想要动态监听Activity状态的地方调用添加或删除对象)
public void subscribeLifeCycle(ActivityLifecycleCallbacks callbacks) {
        this.lifecycleCallbacks.add(callbacks);
    }

    public void unsubscribeLifeCycle(ActivityLifecycleCallbacks callbacks) {
        this.lifecycleCallbacks.remove(callbacks);
    }

参考:

优雅的使用ActivityLifecycleCallbacks管理Activity和区分App前后台
探究 Android 中的 ActivityLifecycleCallbacks

你可能感兴趣的:(Android)