Android應用前後臺切換監聽,藉助ActivityLifecycleCallbacks實現

Dway發表於2018-01-31

有時需要監聽到應用在前後臺切換並做些處理,一般的做法可能是建立一個BaseActivity,然後全部的Activity都繼承它,在BaseActivity的onStart和onStop中計數去處理。這樣並不是最好的方式,不做詳細介紹,有更好的方式,道理其實差不多,就是藉助ActivityLifecycleCallbacks來實現。

一、寫了個幫助類:

package com.dway.helper;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

/**
 * 應用前後臺狀態監聽幫助類,僅在Application中使用
 * Created by dway on 2018/1/30.
 */

public class AppFrontBackHelper {

    private OnAppStatusListener mOnAppStatusListener;


    public AppFrontBackHelper() {

    }

    /**
     * 註冊狀態監聽,僅在Application中使用
     * @param application
     * @param listener
     */
    public void register(Application application, OnAppStatusListener listener){
        mOnAppStatusListener = listener;
        application.registerActivityLifecycleCallbacks(activityLifecycleCallbacks);
    }

    public void unRegister(Application application){
        application.unregisterActivityLifecycleCallbacks(activityLifecycleCallbacks);
    }

    private Application.ActivityLifecycleCallbacks activityLifecycleCallbacks = new Application.ActivityLifecycleCallbacks() {
        //開啟的Activity數量統計
        private int activityStartCount = 0;

        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

        }

        @Override
        public void onActivityStarted(Activity activity) {
            activityStartCount++;
            //數值從0變到1說明是從後臺切到前臺
            if (activityStartCount == 1){
                //從後臺切到前臺
                if(mOnAppStatusListener != null){
                    mOnAppStatusListener.onFront();
                }
            }
        }

        @Override
        public void onActivityResumed(Activity activity) {

        }

        @Override
        public void onActivityPaused(Activity activity) {

        }

        @Override
        public void onActivityStopped(Activity activity) {
            activityStartCount--;
            //數值從1到0說明是從前臺切到後臺
            if (activityStartCount == 0){
                //從前臺切到後臺
                if(mOnAppStatusListener != null){
                    mOnAppStatusListener.onBack();
                }
            }
        }

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

        }

        @Override
        public void onActivityDestroyed(Activity activity) {

        }
    };

    public interface OnAppStatusListener{
        void onFront();
        void onBack();
    }

}


二、Application中使用(注意:僅在Application中才能使用,因為Application的生命週期能監聽到每個Activity)

public class MyApp extends Application {
    
    @Override
    public void onCreate() {
        super.onCreate();

        AppFrontBackHelper helper = new AppFrontBackHelper();
        helper.register(MyApp.this, new AppFrontBackHelper.OnAppStatusListener() {
            @Override
            public void onFront() {
                //應用切到前臺處理

            }

            @Override
            public void onBack() {
                //應用切到後臺處理

            }
        });
    }

}
也可以直接在Application裡面寫,但寫成幫助類,是為了不用每次都去寫,減輕Application的程式碼量

相關文章