Note: The following content is based on Android API Version 27 (Android 8.1) Linux Kernel 3.18.0

Toast show and disappear

Toast to create and display is occurred in the process of App, and Toast when displayed when disappear is controlled by NotificationManagerService.

A Toast has the following process from creation, display and disappearance:

  • throughmakeTextTo create aToastObject, createToastThe object is basically createdToasttheView.
  • callToastThe object’sshowMethod,showMethod calledNotificationManagerServicetheenqueueToastwillToastOne of the internal implementationsITransientNotificationcalledTNtheBinderThe local object is passedNotificationManagerService“And passed it onToastThe duration of.
  • NotificationManagerServiceTo run onsystem_serverProcess, responsible for managing all of the systemToast.NotifactionManagerServiceProcess one by one in the queueToastforToastTo create atoken(new Binder()) and add toWMS(calladdWindowToken),WMSOne will be created for itWindowTokenAnd record thetokenMap.
  • NotifactionManagerServiceThe callbackToasttheTNtheshowMethods,tokenPassed to the App processToastObject.
  • The process of AppToastObject throughHandlerSwitch to main thread executionWindowManager(WindowManagerImpl)theaddViewwillToasttheViewAdded to theWMSTo display itself, including the logic of view rule PL.
  • When the display time arrivesNotificationManagerServiceThe callbackTNthehideMethod is then called by the App processWMSremoveToast.

conclusion

1, NotificationManagerService unified management system in all display and disappear, Toast Toast the real display and disappear operation performed by the process of App Toast object, This is in line with the idea of ActivityManagerService for activities.

2. The window type of Toast is TYPE_TOAST. TYPE_TOAST is a system window, and Toast has its own token and is not controlled by the Activity.

3. Unlike Activity/Dialog, Toast adds its own View directly to WindowManagerImpl without creating a PhoneWindow or DecorView. Toast View events (if any) are sent directly to the Toast View Tree.