Androider

android 前后台切换监听

2016-06-29  本文已影响1018人  许方镇

本文为【viclee】原创,~原文连接

当我们从activity A到Activity B时,执行的生命周期的执行顺序如下:
A.onPause -> B.onCreate -> B.onStart -> B.onResume -> A.onStop
如果在BaseActivity里设置个count,初始值为0。然后在onStart 或者 onResume对count加1,在onStop中减1。

可知

通过count可以知道在前后台,在API14之前是用这种方法,之后出现了ActivityLifecycleCallbacks方法,看这名字就知道是监听activity的生命周期的;

    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);
    }

可以在Application的onCreate方法里注册下监听所有的activity

registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
    ……
}

在项目中具体用法:

先实现一个ActivityLifecycleCallbacks类

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

/**
 * Created by xufangzhen on 2016/6/23.
 */
public class BaseTaskSwitch implements Application.ActivityLifecycleCallbacks {

    public int mCount = 0;
    private OnTaskSwitchListener mOnTaskSwitchListener;
    private static BaseTaskSwitch mBaseLifecycle;

    public static BaseTaskSwitch init(Application application) {
        if (null == mBaseLifecycle) {
            mBaseLifecycle = new BaseTaskSwitch();
            application.registerActivityLifecycleCallbacks(mBaseLifecycle);
        }
        return mBaseLifecycle;
    }

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

    @Override
    public void onActivityStarted(Activity activity) {
        if (mCount++ == 0) {
            mOnTaskSwitchListener.onTaskSwitchToForeground();
        }
    }

    @Override
    public void onActivityResumed(Activity activity) {}

    @Override
    public void onActivityPaused(Activity activity) {}

    @Override
    public void onActivityStopped(Activity activity) {
        if (--mCount == 0) {
            mOnTaskSwitchListener.onTaskSwitchToBackground();
        }
    }

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

    @Override
    public void onActivityDestroyed(Activity activity) {}
    
    public void setOnTaskSwitchListener(OnTaskSwitchListener listener) {
        mOnTaskSwitchListener = listener;
    }

    public interface OnTaskSwitchListener {

        void onTaskSwitchToForeground();

        void onTaskSwitchToBackground();
    }
}

然后在你的Application子类中的onCreate方法中调用:

    @Override
    public void onCreate() {
        super.onCreate();
        //前后台切换
        BaseTaskSwitch.init(this).setOnTaskSwitchListener(new BaseTaskSwitch.OnTaskSwitchListener() {
            @Override
            public void onTaskSwitchToForeground() {
                LogUtils.d("切换到前台");
            }

            @Override
            public void onTaskSwitchToBackground() {
                LogUtils.d("切换到后台");
            }
        });
    }
上一篇下一篇

猜你喜欢

热点阅读