赞
踩
- @HiltAndroidApp
- class MyApplication :Application{
- constructor() : super()
- override fun onCreate() {
- super.onCreate()
- Log.e("MyApplication","=====MyApplication")
- registerActivityLifecycleCallbacks(activityLifecycleCallbacks)
- }
-
-
- val activityLifecycleCallbacks:ActivityLifecycleCallbacks= object:ActivityLifecycleCallbacks{
-
- private var activityStartCount = 0
- override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
- }
-
- override fun onActivityStarted(activity: Activity) {
- activityStartCount++
- if(activityStartCount==1){
- //从后台切换到前台
- }
-
- }
-
- override fun onActivityResumed(activity: Activity) {
- }
-
- override fun onActivityPaused(activity: Activity) {
- }
-
- override fun onActivityStopped(activity: Activity) {
- activityStartCount--
- if (activityStartCount == 0){
- //从前台切换到后台
- }
- }
-
- override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
- }
-
- override fun onActivityDestroyed(activity: Activity) {
- }
-
- }
- }
registerActivityLifecycleCallbacks(activityLifecycleCallbacks)这个是监听所有的activity的生命周期的,为什么能做到呢?接下来我们慢慢分析。
ActivityLifecycleCallbacks是Application一个内部的接口。
Application提供了
registerActivityLifecycleCallbacks(ActivityLifecycleCallbacks callback)
unregisterActivityLifecycleCallbacks(ActivityLifecycleCallbacks callback)
两个方法。
我们分析其中registerActivityLifecycleCallbacks()方法
- public void registerActivityLifecycleCallbacks(ActivityLifecycleCallbacks callback) {
- synchronized (mActivityLifecycleCallbacks) {
- mActivityLifecycleCallbacks.add(callback);
- }
- }
发现是将接口的实现添加到Application的一个集合中
private ArrayList<ActivityLifecycleCallbacks> mActivityLifecycleCallbacks = new ArrayList<ActivityLifecycleCallbacks>();
这又是一个典型的观察者模式。
那这个集合肯定是在哪里调用的呢?全局搜索发现在这个方法中转成了数组
- @UnsupportedAppUsage
- private Object[] collectActivityLifecycleCallbacks() {
- Object[] callbacks = null;
- synchronized (mActivityLifecycleCallbacks) {
- if (mActivityLifecycleCallbacks.size() > 0) {
- callbacks = mActivityLifecycleCallbacks.toArray();
- }
- }
- return callbacks;
- }
这个数组又在application中的
dispatchActivityCreated
dispatchActivityStarted
dispatchActivityResumed
等方法中被调用,如下所示:
- /* package */ void dispatchActivityDestroyed(@NonNull Activity activity) {
- Object[] callbacks = collectActivityLifecycleCallbacks();
- if (callbacks != null) {
- for (int i=0; i<callbacks.length; i++) {
- ((ActivityLifecycleCallbacks)callbacks[i]).onActivityDestroyed(activity);
- }
- }
- }
上面的几个方法明显对应的是activity的生命周期,那么很显然这些方法肯定是得在activity的生命周期中调用,不然是无法监听activity的生命周期的。我们到activity的最parent的Activity中去寻找。
我们分析其中
Resume的一个生命周期(其他的大同小异),如下所示:
- protected void onResume() {
- if (DEBUG_LIFECYCLE) Slog.v(TAG, "onResume " + this);
- dispatchActivityResumed(); //调用这方法
- mActivityTransitionState.onResume(this);
- enableAutofillCompatibilityIfNeeded();
- if (mAutoFillResetNeeded) {
- if (!mAutoFillIgnoreFirstResumePause) {
- View focus = getCurrentFocus();
- if (focus != null && focus.canNotifyAutofillEnterExitEvent()) {
- // TODO(b/148815880): Bring up keyboard if resumed from inline authentication.
- // TODO: in Activity killed/recreated case, i.e. SessionLifecycleTest#
- // testDatasetVisibleWhileAutofilledAppIsLifecycled: the View's initial
- // window visibility after recreation is INVISIBLE in onResume() and next frame
- // ViewRootImpl.performTraversals() changes window visibility to VISIBLE.
- // So we cannot call View.notifyEnterOrExited() which will do nothing
- // when View.isVisibleToUser() is false.
- getAutofillManager().notifyViewEntered(focus);
- }
- }
- }
-
- notifyContentCaptureManagerIfNeeded(CONTENT_CAPTURE_RESUME);
-
- mCalled = true;
- }
果然是调用了Application中对象的方法
-
- // getApplication().dispatchActivityResumed(this); 调用了Application中的分发方法
-
- private void dispatchActivityResumed() {
- getApplication().dispatchActivityResumed(this);
- Object[] callbacks = collectActivityLifecycleCallbacks();
- if (callbacks != null) {
- for (int i = 0; i < callbacks.length; i++) {
- ((Application.ActivityLifecycleCallbacks) callbacks[i]).onActivityResumed(this);
- }
- }
- }
2.利用Lifecycle的扩展方法
添加依赖:
- //只能适用于androidX项目
- implementation("androidx.lifecycle:lifecycle-process:2.2.0")
在mainActivity或者application中添加如下代码:
- ProcessLifecycleOwner.get().lifecycle.addObserver(object:DefaultLifecycleObserver{
- override fun onStop(owner: LifecycleOwner) {
- super.onStop(owner)
- //后台
- }
-
-
- override fun onStart(owner: LifecycleOwner) {
- super.onStart(owner)
- //前台
- }
-
- })
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。