问题
I have read the standard Windows-related documentation and pawed through a bunch of the source code in an attempt to understand how and when Android Windows are created. I believe I have my arms around it and would like to have it verified or corrected.
To my knowledge, there are only two ways to get a handle to a Window object.
1. Activity's getWindow.
2. Dialog's getWindow method.
In both of the above cases, you can get a handle to the Window using getWindow and then manipulate the Window using the handle.
Windows can also be created by the use of WindowManager's addView method, but it isn't possible to get a handle to such Windows. This is a very confusing area because the addView method itself doesn't imply creating a Window and even the comment for it in the View Manager source code just says the following.
Assign the passed LayoutParams to the passed View and add the view to
the window.
So it appears that the method is imply adding a View to your existing Window. However, the second parameter to addView is a WindowManager.LayoutParams instance which, among other things, specifies a Window type (e.g., TYPE_SYSTEM_ALERT), implying that a Window is actually getting created. This is, in fact, the case. Here's a very brief outline of what happens in the source code. (For newbies: you can browse source code on many different web sites; my favorite is http://grepcode.com.)
WindowManager implements the ViewManager interface, which is where addView is defined. URL: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/view/ViewManager.java#ViewManager.addView%28android.view.View%2Candroid.view.ViewGroup.LayoutParams%29
The actual implementation of the WindowManager class is called WindowManagerImpl. It's addView calls WindowManagerGlobal's addView. URL: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/view/WindowManagerImpl.java#83
WindowManagerGlobal's addView is where the real work gets done. URL: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/view/WindowManagerGlobal.java#WindowManagerGlobal.addView%28android.view.View%2Candroid.view.ViewGroup.LayoutParams%2Candroid.view.Display%2Candroid.view.Window%29
- Verifies that the params passed in are a WindowManager.LayoutParams.
- Adjusts the layout parameters if the View has a parent.
- Determines if the new View (aka Window) is a panel Window (i.e., a Sub-window) or not and deals with that.
- Creates a new ViewRootImpl.
- Adds the View, ViewRootImpl and WindowManager.LayoutParams instances to separate ArrayLists.
- Adds the View and associated params to the ViewRootImpl via the latter's setView method.
ViewRootImpl's setView method then does a bunch of low-level work. It throws exceptions with variations on "Unable to add window..." messages in various error situations. According to the main comment for ViewRootImpl, it is "The top of a view hierarchy, implementing the needed protocol between View and the WindowManager. This is for the most part an internal implementation detail of WindowManagerGlobal."
URL: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/view/ViewRootImpl.java#ViewRootImpl.setView%28android.view.View%2Candroid.view.WindowManager.LayoutParams%2Candroid.view.View%29
WindowManagerGlobal appears to track multiple Windows via the three arrays. As far as I can tell, the ViewRootImpl is effectively the new window and it is managed by WindowManagerGlobal.
Based on the above, it appears that calling addView does, in fact, create a new window (although it is variously referred to as a view and a window) but no associated Window class is provided to access it, so the developer can't get a handle to it.
My questions are:
- Are there ways to get a handle to a Window instance, other than the two getWindow methods mentioned above?
- Does addView, in fact, create a Window and, if so, is there any way to get a handle to it?
- Is anything else above incorrect and, if so, in what respect(s)?
Thanks! Barry
来源:https://stackoverflow.com/questions/34186706/android-windows-when-and-how-are-they-created