利用ActivityLifecycleCallbacks判断APP是否在后台运行

Android 在SDK 14提供了一个Callback即ActivityLifecycleCallbacks,在Application中通过注册这个Callback可拿到App所有Activity的生命周期回调。解除注册可以调用Application中的unregisterActivityLifecycleCallbacks。

Application.java

...

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

...

Demo

CustomApplication.java

public class CustomApplication extends Application {

    private static CustomApplication instance;

    // Android 生命周期监听
    private ActivityLifeListener activityLifeListener;

    // 可见 Activity 个数
    private int activityCount;

    // 判断是否已经显示对话框
    private boolean isShowDialog;

    @Override
    public void onCreate() {
        super.onCreate();
        CustomApplication.instance = this;

        activityLifeListener = new ActivityLifeListener();
        this.registerActivityLifecycleCallbacks(activityLifeListener);
    }

    public static CustomApplication getInstance() {
        return instance;
    }

    /**
     * 判断 App 是否在前台运行
     */
    public boolean isBackground() {
        return activityCount == 0;
    }

    private class ActivityLifeListener implements ActivityLifecycleCallbacks{

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

        @Override
        public void onActivityStarted(Activity activity) {
            activityCount++;
        }

        @Override
        public void onActivityResumed(Activity activity) {
            if (!isShowDialog) {
                isShowDialog = true;
                new AlertDialog.Builder(activity)
                        .setTitle("提示")
                        .setMessage("这个对话框只有按Home键之后再回来才显示")
                        .setPositiveButton("确定", null)
                        .create()
                        .show();
            }
        }

        @Override
        public void onActivityPaused(Activity activity) {}

        @Override
        public void onActivityStopped(Activity activity) {
            activityCount--;
            if (isBackground()) {
                // initialization something
                isShowDialog = false;
            }
        }

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

        @Override
        public void onActivityDestroyed(Activity activity) {}

    }

}

Activity的生命周期

利用ActivityLifecycleCallbacks判断APP是否在后台运行_第1张图片

  • HOME键的执行顺序:onPause->onStop->onRestart->onStart->onResume.
  • BACK键的顺序: onPause->onStop->onDestroy->onCreate->onStart->onResume.
  • onPause不要做太耗时的工作.
  • onCreate 与 onDestory 匹配.
  • onStart 与 onStop 匹配.
  • onResume 与 onResume 匹配.
  • onStart 与 onResume 都表示在前台运行, 区别在于 onStart 不可见, onResume 可见.

参考资料:
1. http://blog.csdn.net/lfdfhl/article/details/51045414

你可能感兴趣的:(android)