Deep understanding of window and WindowManager in Android

Window represents the concept of a window. Window is an abstract class, and its concrete implementation is phonewindow. Creating a window can be completed through WindowManager. WindowManager is the entrance for the outside world to access the window. The specific implementation of window is located in windowmanagerservice. The interaction between WindowManager and windowmanagerservice is an IPC process. In Android, all views are rendered through window. Whether they are activity, dialog or toast, their views are actually attached to the window. Therefore, window is the direct manager of the actual view. The click event is passed from window to decorview, and then from decorview to our view, Even the setting view method setcontentview of activity is completed through window at the bottom.

Windows and WindowManager

The key codes in the process of adding a window are:

There are two flags and type parameters in windowmanager.layoutparams.

The flags parameter has three window attributes

The type parameter indicates the type of window. There are three types: application window, child window and system window. The application class window corresponds to an activity. The child window cannot exist alone. It needs to be attached to a specific parent window. For example, the common dialog is a child window, and the system window needs to declare permissions in the window that can be created, For example, toast and system status bar are system windows.

Windows are hierarchical. Each window has a corresponding z-ordered. Windows with a large level will cover windows with a small level. Among the three types of windows, the level range of application window is 1-99, the level range of sub window is 1000-1999, and the level range of system window is 2000-2999. These level ranges correspond to the type parameter of windowmanager.layoutparams. If you want windows to be at the top level of all windows, you can use a larger level. Obviously, the system window level is the largest, and the system level has many values.

The functions provided by WindowManager are very simple. There are three common methods: add view, update view and delete view. These three methods are defined in viewmanager, and WindowManager inherits viewmanager.

Internal mechanism of window

Window is an abstract concept. Each window corresponds to a view and a viewrootimpl. Window and view are connected through viewrootimpl, which shows that view is the entity of window and cannot directly access window in actual use. Access to window must be through WindowManager.

Window addition process

The addition process of window needs to be implemented through the addview of WindowManager. WindowManager is an interface, and its real implementation is the windowmanagerimpl class.

It can be seen that windowmanagerimpl does not directly implement the three major operations of window, but all of them are handed over to windowmanagerglobal. Windowmanagerglobal provides its own instances in the form of factories. The addview method of windowmanagerglobal is mainly divided into the following steps:

Window deletion process

The deletion process of window is the same as the addition process. It is implemented first through windowmanagerimpl and then through windowmanagerglobal. There is an internal implementation of the dispatchdetachedfromwindow method, which mainly does four things:

Garbage collection related work, such as clearing data and messages and removing callbacks

Delete the window through the remove method of session

When the dispatchdetachedfromwindow method of view is called, the ondetachedfromwindow() and ondetachedfromwindowinternal() of view will be called internally

Call the doremoveview method of windowmanagerglobal to refresh the data

Window update process

The updateviewlayout method is mainly used. First, it needs to update the layoutparams of the view and replace the old layoutparams, and then update the layoutparams in viewrootimpl. This step is realized through the setlayoutparams method of viewrootimpl. In viewrootimpl, the view will be rearranged through the scheduletraversals method, including measurement, layout and redrawing.

Window creation process

View is a view rendering method in Android, but view cannot exist alone. It must be attached to the abstract concept of window. Therefore, where there is a view, there is window.

Window creation process of activity

How to create an activity needs to understand the startup process of the activity, which is complex, but it is finally completed by perfromlaunchactivity() in activitythred. In this method, the instance object of the activity will be created through the class loader, and its attach method will be called to associate a series of context variables that the operation depends on.

In the attach method of activity, the system will create the window object to which the activity belongs and set the callback interface. The creation of window object is realized through the makenewwindow method of policymanager. It can be seen from the implementation of setcontentview of activity that the specific implementation of activity is handed over to window, and the specific implementation of window is phonewindow, Therefore, you only need to look at the relevant logic of phonewindow. The steps are as follows:

After the above three steps, the decorview has been created and initialized, and the layout file of the activity has been successfully added to the mcontentparent of the decorview. However, at this time, the decorview has not been officially added to the window by WindowManager. The view is really called in the onresume method of the activity, and then the makevisible() of the activity will be called, It is in the makevisible method that decorview really completes the two processes of adding and displaying.

Window creation process of dialog

The creation process of dialog window is similar to that of activity. There are the following steps:

An ordinary dialog has a special feature, that is, the context of activity must be used. If the context of application is used, an error will be reported.

Toast window creation process

Toast is different from dialog in that its working process is slightly complicated. Firstly, toast is also implemented based on window, but because toast has the function of timing cancellation, the system adopts handler. There are two types of IPC processes inside toast. The first is that toast accesses notificationmanagerservice, and the second is the TN interface of notificationmanagerservice callback toast.

Toast belongs to the system window. Its internal views can be specified in two ways: one is the default style of the system, and the other is to specify a custom view through the setview method. In any case, they all correspond to an internal member mnextview of a view type of toast. Toast provides show and cancel to show and hide toast respectively. Their internal is an IPC process.

The above is Xiaobian's introduction to windows and WindowManager in Android. I hope it will help you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>