SoFunction
Updated on 2025-04-10

How to implement pop-up window order & priority control on Android

Generally, on the homepage of the project, there are often multiple dialog boxes that need to pop up, such as activity popups, update popups, rating popups, etc., and these popups have priority order. These pop-up windows are generally returned to the result after requesting through the interface. If there are only a few pop-up windows, it is easy to process. Just judge the business logic to display them one after another. If there are more than a dozen or more, it will be very troublesome to deal with and prone to problems.

Therefore, it is very necessary to encapsulate a pop-up function that can be displayed in priority order. First of all, the functional requirements are as follows:

  • Display various pop-up windows in order of priority, and the default display starts from the highest priority.
  • Only if the previous high priority pop-up window is displayed or cancelled, the next low priority pop-up window can be displayed.
  • The prerequisite for specifying the display of a pop-up window is that there is no higher priority pop-up window to be displayed.
  • Before displaying a pop-up window, you need to determine whether it can or needs to be displayed.
  • Find the specified pop-up window according to the priority level, the priority level is equivalent to the unique ID
  • Popup windows include multiple types, Dialog, PopupWindow, Activity, etc.

Then start encoding to implement functions, first determine an enumeration class, listing the supported pop-up window types, including Dialog, PopupWindow, Activity, etc.

public enum WindowType {

  DIALOG,
  POUPOWINDOW,
  TOAST,
  SNACKBAR,
  WIDGET,
  ACTIVITY,
  OTHERS

}

Then define the pop-up interface IWindow, which defines the basic functions of pop-up windows.

/**
 * Window convention rules
 */
public interface IWindow {

  /**
    * Pop-up window display
    */
  void show(Activity activity);

  /**
    * Pop-up window closes
    */
  void dismiss();

  /**
    * Set the window to close the listening
    */
  void setOnWindowDismissListener(OnWindowDismissListener listener);

  /**
    * Set window display monitoring
    */
  void setOnWindowShowListener(OnWindowShowListener listener);

}

and the listening interface that pop-up window displays and closes,

/**
 * The window closes the listening
 */
public interface OnWindowDismissListener {

  /**
   *
   */
  void onDismiss();

}

/**
 * Window display monitoring
 */
public interface OnWindowShowListener {

  void onShow();

}

Next, define a package class WindowWrapper to encapsulate pop-up properties and statuses, including pop-up windows, priority, whether it can be displayed, form type, etc., which will be used when processing pop-up window display logic.

/**
 * Window parameter class
 */
public class WindowWrapper {

  /**
    * Window
    */
  private IWindow mWindow;

  /**
    * Priority, the higher the value, the higher the priority
    */
  private int mPriority;

  /**
    * Is it currently in show status
    */
  private boolean isShowing;

  /**
    * Whether the show conditions are met
    */
  private boolean isCanShow;

  /**
    * Pop-up window type
    */
  private WindowType mWindowType;

  /**
    * Pop-up window name
    */
  private String mWindowName;

  private WindowWrapper(Builder builder) {
    mWindow = ;
    mPriority = ;
    mWindowType = ;
    isCanShow = ;
    mWindowName = ;
  }

  public IWindow getWindow() {
    return mWindow;
  }

  public void setWindow(IWindow window) {
     = window;
  }

  public int getPriority() {
    return mPriority;
  }

  public void setPriority(int priority) {
     = priority;
  }

  public boolean isShowing() {
    return isShowing;
  }

  public void setShowing(boolean showing) {
    isShowing = showing;
  }

  public WindowType getWindowType() {
    return mWindowType;
  }

  public void setWindowType(WindowType mWindowType) {
     = mWindowType;
  }

  public boolean isCanShow() {
    return isCanShow;
  }

  public void setCanShow(boolean canShow) {
    isCanShow = canShow;
  }

  public String getWindowName() {
    return mWindowName;
  }

  public void setWindowName(String mWindowName) {
     = mWindowName;
  }

  public static class Builder {

    /**
      * Window
      */
    private IWindow window;

    /**
      * Priority, the higher the value, the higher the priority
      */
    private int priority;

    /**
      * Pop-up window type
      */
    private WindowType windowType;

    /**
      * Whether the show conditions are met
      */
    private boolean isCanShow;

    /**
      * Pop-up window name
      */
    private String windowName;

    public Builder window(IWindow window) {
       = window;
      return this;
    }

    public Builder priority(int priority) {
       = priority;
      return this;
    }

    public Builder windowType(WindowType type) {
       = type;
      return this;
    }

    public Builder setCanShow(boolean canShow) {
      isCanShow = canShow;
      return this;
    }

    public String getWindowName() {
      return windowName;
    }

    public Builder setWindowName(String windowName) {
       = windowName;
      return this;
    }

    public WindowWrapper build() {
      return new WindowWrapper(this);
    }
  }
}

Finally, through the WindowTaskManager class, we can organize the addition, display, and closing of pop-up windows.

public class WindowTaskManager {
  private List<WindowWrapper> mWindows;


  private static WindowTaskManager mDefaultInstance;


  private WindowTaskManager() {
  }


  /**
    * Get pop-up window manager
    */
  public static WindowTaskManager getInstance() {
    if (mDefaultInstance == null) {
      synchronized () {
        if (mDefaultInstance == null) {
          mDefaultInstance = new WindowTaskManager();
        }
      }
    }
    return mDefaultInstance;
  }


  /**
    * Add pop-up window
    *
    * @param windowWrapper Popup window to be displayed
    */
  public synchronized void addWindow(Activity activity, WindowWrapper windowWrapper) {
    if (windowWrapper != null) {
      if (mWindows == null) {
        mWindows = new ArrayList<>();
      }


      if (() != null) {
        ().setOnWindowShowListener(new OnWindowShowListener() {
          @Override
          public void onShow() {
            (true);
          }
        });


        ().setOnWindowDismissListener(new OnWindowDismissListener() {
          @Override
          public void onDismiss() {
            (false);
            (windowWrapper);
            showNext(activity);
          }
        });
      }


      (windowWrapper);
    }
  }


  /**
    * The pop-up window meets the display conditions
    *
    * @param priority
    */
  public synchronized void enableWindow(Activity activity, int priority, IWindow window) {
    WindowWrapper windowWrapper = getTargetWindow(priority);
    if (windowWrapper != null) {


      if (() == null) {
        (new OnWindowShowListener() {
          @Override
          public void onShow() {
            (true);
          }
        });


        (new OnWindowDismissListener() {
          @Override
          public void onDismiss() {
            (false);
            (windowWrapper);
            showNext(activity);
          }
        });
      }


      (true);
      (window);
      show(activity, priority);
    }
  }


  /**
    * Remove pop-up windows that do not need to be displayed
    *
    * @param priority
    */
  public synchronized void disableWindow(int priority) {
    WindowWrapper windowWrapper = getTargetWindow(priority);
    if (windowWrapper != null && () != null) {
      if (mWindows != null) {
        (windowWrapper);
      }
    }
  }


  /**
    * Display pop-up window
    * Start displaying from the highest priority Window
    */
  public synchronized void show(Activity activity) {
    WindowWrapper windowWrapper = getMaxPriorityWindow();
    if (windowWrapper != null && ()) {
      IWindow window = ();
      if (window != null) {
        (activity);
      }
    }
  }


  /**
    * Display the specified pop-up window
    *
    * @param priorities
    */
  public synchronized void show(Activity activity, int priorities) {
    WindowWrapper windowWrapper = getTargetWindow(priorities);
    if (windowWrapper != null && () != null) {
      WindowWrapper topShowWindow = getShowingWindow();
      if (topShowWindow == null) {
        int priority = ();
        WindowWrapper maxPriorityWindow = getMaxPriorityWindow();
        if (maxPriorityWindow != null && () && priority >= ()) {
          if (() != null) {
            ().show(activity);
          }
        }
      }
    }
  }


  /**
    * Clear pop-up window manager
    */
  public synchronized void clear() {
    if (mWindows != null) {
      for (int i = 0, size = (); i < size; i++) {
        if ((i) != null) {
          IWindow window = (i).getWindow();
          if (window != null) {
            ();
          }
        }
      }
      ();
    }
    ().onDestroy();
  }


  /**
    * Clear pop-up window manager
    *
    * @param dismiss Whether to dismiss the pop-up window maintained by the pop-up window manager at the same time
    */
  public synchronized void clear(boolean dismiss) {
    if (mWindows != null) {
      if (dismiss) {
        for (int i = 0, size = (); i < size; i++) {
          if ((i) != null) {
            IWindow window = (i).getWindow();
            if (window != null) {
              ();
            }
          }
        }
      }
      ();
    }
    ().onDestroy();
  }


  /**
    * Show the next Window with the highest priority
    */
  private synchronized void showNext(Activity activity) {
    WindowWrapper windowWrapper = getMaxPriorityWindow();
    if (windowWrapper != null && ()) {
      if (() != null) {
        ().show(activity);
      }
    }
  }


  /**
    * Get the Window with the highest priority in the current stack (the pop-up window will be added if the priority is the same)
    */
  private synchronized WindowWrapper getMaxPriorityWindow() {
    if (mWindows != null) {
      int maxPriority = -1;
      int position = -1;
      for (int i = 0, size = (); i < size; i++) {
        WindowWrapper windowWrapper = (i);
        if (i == 0) {
          position = 0;
          maxPriority = ();
        } else {
          if (() >= maxPriority) {
            position = i;
            maxPriority = ();
          }
        }
      }
      if (position != -1) {
        return (position);
      } else {
        return null;
      }
    }
    return null;
  }


  private synchronized WindowWrapper getTargetWindow(int priority) {
    if (mWindows != null) {
      for (int i = 0, size = (); i < size; i++) {
        WindowWrapper windowWrapper = (i);
        if (windowWrapper != null && () == priority) {
          return windowWrapper;
        }
      }
    }
    return null;
  }


  /**
    * Get pop-up window currently in show state
    */
  private synchronized WindowWrapper getShowingWindow() {
    if (mWindows != null) {
      for (int i = 0, size = (); i < size; i++) {
        WindowWrapper windowWrapper = (i);
        if (windowWrapper != null && () != null && ()) {
          return windowWrapper;
        }
      }
    }
    return null;
  }

}

There are three main methods of the WindowTaskManager class:

  • addWindow(Activity activity, WindowWrapper windowWrapper)
  • enableWindow(Activity activity, int priority, IWindow window)
  • disableWindow(int priority)

The dialog boxes that need to be displayed in order are added using the addWindow method, which must be called before the network request is made. The function is to tell WindowTaskManager how many pop-up windows need to be displayed in order. After the network request returns, if you need to display the pop-up window, call the enableWindow method to display it. If you don't need to display the pop-up window, call the disableWindow method to remove the pop-up window from the display queue.

The above is the main logic for displaying pop-up windows in order. If you use it, the form will first inherit IWindow to implement the relevant methods. Then, by operating the WindowTaskManager class. For specific usage methods, please refer to the source code.

Project address:/Geekince/Pr…

Easter eggs:

When you need to display DialogFragment in DialogFragment, it is best not to start the display directly in DialogFragment, but to start the display in DialogFragment's disappearing callback. Because when a DialogFragment disappears, getChildFragmentManager may fail, getFragmentManager should be used on the outer layer.

The above is the detailed content of how Android implements pop-up order and priority control. For more information on Android implementing pop-up order and priority control, please pay attention to my other related articles!