How does ActivityLifecycleCallbacks determine if APP is in the foreground

  • 2021-09-20 21:34:07
  • OfStack

This article shares how ActivityLifecycleCallbacks judges whether APP is in the front desk for your reference. The specific contents are as follows

ActivityManager

Usually, we judge whether app is in the foreground through ActivityManager.


/**
   * Activity Is it at the front desk 
   * @param context
   * @return
   */
  private boolean isOnForground(Context context){
    ActivityManager activityManager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> appProcessInfoList = activityManager.getRunningAppProcesses();
    if(appProcessInfoList == null){
      return false;
    }

    String packageName = context.getPackageName();
    for(ActivityManager.RunningAppProcessInfo processInfo : appProcessInfoList){
      if(processInfo.processName.equals(packageName) && processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND ){
        return true;
      }
    }
    return false;
  }

But this is not the best:

Poor performance, which is equivalent to traversing all processes to find the foreground and the package name matches. It doesn't apply on some mobile phones.

ActivityLifecycleCallbacks

Application can register the ActivityLifecycleCallbacks interface through registerActivityLifecycleCallbacks to implement a callback to all Activity lifecycle.


this.registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
      @Override
      public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

      }

      @Override
      public void onActivityStarted(Activity activity) {

      }

      @Override
      public void onActivityResumed(Activity activity) {

      }

      @Override
      public void onActivityPaused(Activity activity) {

      }

      @Override
      public void onActivityStopped(Activity activity) {

      }

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

      }

      @Override
      public void onActivityDestroyed(Activity activity) {

      }
    });

Every Activity lifecycle will call back the corresponding method. It means that Application can monitor the life cycle of all Activity, isn't it very cow x?
This function can be used to do some statistics, or make an Activity linked list to know the Activity before and after, although this demand has not been encountered yet, of course, the function of "judging whether APP is in the foreground" is not too much.

Life cycle

Then, in order to achieve the function of "judging whether APP is in the foreground", we need to know the life cycle of activity first. Isn't it simple?
However, many people don't know how the life cycles of ActivityA intent will switch if ActivityB jumps to ActivityB.
Publish the answer:
ActivityA. OnPause ()-- > AcitivityB. OnCreate ()-- > AcitivityB. OnStart ()-- > ActivityB. OnResume ()-- > ActivityA.OnStop()
In fact, it should be like this. If you want to switch to a new interface, you have to wait for the new interface to be displayed before you can deal with the old interface. You can't dispose of the old one first, and then load the new interface with a black screen.

Determine whether APP is in the foreground


this.registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
      @Override
      public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

      }

      @Override
      public void onActivityStarted(Activity activity) {
        count++;
        if(count == 1){
          Log.e("ZXK","foreground");
        }
      }

      @Override
      public void onActivityResumed(Activity activity) {

      }

      @Override
      public void onActivityPaused(Activity activity) {

      }

      @Override
      public void onActivityStopped(Activity activity) {
        count--;
        if(count == 0){
          Log.e("ZXK","background");
        }
      }

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

      }

      @Override
      public void onActivityDestroyed(Activity activity) {

      }
    });
Create a new int variable count Then register the callback in Application. onCreate () count+1 in onActivityStarted (), if count is 1, enter the foreground; count-1 in onActivityStopped (), if count is 0, it enters the background.

Related articles: