title: Android Windows: When and how are they created?
body: <p>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.</p>
  
  <p>To my knowledge, there are only two ways to get a handle to a Window object.</p>
  
  <pre><code>1. Activity's getWindow.
  2. Dialog's getWindow method.
  </code></pre>
  
  <p>In both of the above cases, you can get a handle to the Window using getWindow
  and then manipulate the Window using the handle.</p>
  
  <p>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.</p>
  
  <pre><code>Assign the passed LayoutParams to the passed View and add the view to
  the window.
  </code></pre>
  
  <p>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 <a href="http://grepcode.com" rel="noreferrer">http://grepcode.com</a>.)</p>
  
  <ol>
  <li><p>WindowManager implements the ViewManager interface, which is where
   addView is defined.
   URL: <a href="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" rel="noreferrer">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</a></p></li>
  <li><p>The actual implementation of the WindowManager class is called
   WindowManagerImpl. It's addView calls WindowManagerGlobal's addView.
   URL: <a href="http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/view/WindowManagerImpl.java#83" rel="noreferrer">http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/view/WindowManagerImpl.java#83</a></p></li>
  <li><p>WindowManagerGlobal's addView is where the real work gets done.
   URL: <a href="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" rel="noreferrer">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</a></p>
  
  <ol>
  <li>Verifies that the params passed in are a WindowManager.LayoutParams.</li>
  <li>Adjusts the layout parameters if the View has a parent.</li>
  <li>Determines if the new View (aka Window) is a panel Window
  (i.e., a Sub-window) or not and deals with that.</li>
  <li>Creates a new ViewRootImpl.</li>
  <li>Adds the View, ViewRootImpl and WindowManager.LayoutParams instances
  to separate ArrayLists.</li>
  <li>Adds the View and associated params to the ViewRootImpl via the
  latter's setView method.</li>
  </ol></li>
  <li><p>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." </p>
  
  <p>URL: <a href="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" rel="noreferrer">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</a></p></li>
  </ol>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>My questions are:</p>
  
  <ol>
  <li>Are there ways to get a handle to a Window instance, other than the
   two getWindow methods mentioned above?</li>
  <li>Does addView, in fact, create a Window and, if so, is there any way
   to get a handle to it?</li>
  <li>Is anything else above incorrect and, if so, in what respect(s)?</li>
  </ol>
  
  <p>Thanks!
  Barry</p>