SoFunction
Updated on 2025-03-11

How to determine whether the APP is in the front desk

This article shares with you how to determine whether the APP is in the front desk for your reference. The specific content is as follows

ActivityManager

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

/**
    * Is the Activity in the front desk?
    * @param context
    * @return
    */
  private boolean isOnForground(Context context){
    ActivityManager activityManager = (ActivityManager) (ACTIVITY_SERVICE);
    List<> appProcessInfoList = ();
    if(appProcessInfoList == null){
      return false;
    }

    String packageName = ();
    for( processInfo : appProcessInfoList){
      if((packageName) &&  == .IMPORTANCE_FOREGROUND ){
        return true;
      }
    }
    return false;
  }

But this is not the best:

  • Poor performance is equivalent to traversing all processes to find the foreground and the package name matches.
  • Not applicable on some phones.

ActivityLifecycleCallbacks

Application can register the ActivityLifecycleCallbacks interface through registerActivityLifecycleCallbacks to implement callbacks to all Activity lifecycles.

(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) {

      }
    });

The life cycle of each activity will call back the corresponding method. It means that Application can monitor the life cycle of all activities, isn't it very awesome?
This function can be used to do some statistics, or to make it into an Activity linked list to know the activities before and after. Although I have not encountered such a requirement yet, of course, it is not an exaggeration to determine whether the APP is in the front desk.

life cycle

Then, in order to achieve the function "judging whether the APP is in the foreground", we need to first know the life cycle of the activity. Isn't it very simple?
But many people don’t know how the life cycles of these two are switched if ActivityA intent jumps to ActivityB.
Announced answer:
() —> () —> () —> () —> ()
In fact, this is indeed the case. If you have to switch to the new interface, you have to wait until the new interface is displayed before you can deal with the old interface. You can't just process the old ones first and then load the new interface with a black screen.

Determine whether the APP is in the front desk

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

      }

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

      @Override
      public void onActivityResumed(Activity activity) {

      }

      @Override
      public void onActivityPaused(Activity activity) {

      }

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

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

      }

      @Override
      public void onActivityDestroyed(Activity activity) {

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

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.