public class BaseApplication extends Application { private static BaseApplication instance; /** * 当前Activity个数 */ private int activityNumber = 0; @Override public void onCreate() { super.onCreate(); registerActivityLifecycleCallbacks(activityLifecycleCallbacks); instance = this; } public static BaseApplication getInstance() { return instance; } /** * Activity 生命周期监听,用于监控app前后台状态切换 */ ActivityLifecycleCallbacks activityLifecycleCallbacks = new ActivityLifecycleCallbacks() { @Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) { } @Override public void onActivityStarted(Activity activity) { if (activityNumber == 0) { LogUtil.i("app回到前台"); } activityNumber++; LogUtil.i("activityNumber = " + activityNumber); } @Override public void onActivityResumed(Activity activity) { } @Override public void onActivityPaused(Activity activity) { } @Override public void onActivityStopped(Activity activity) { activityNumber--; LogUtil.i("activityNumber = " + activityNumber); if (activityNumber == 0) { // app回到后台 LogUtil.i("app回到后台"); } } @Override public void onActivitySaveInstanceState(Activity activity, Bundle outState) { } @Override public void onActivityDestroyed(Activity activity) { } }; }