title: Correct usage of WindowManager.removeViewImmediate()
body: <p>The documentation for <a href="https://developer.android.com/reference/android/view/WindowManager.html#removeViewImmediate(android.view.View)" rel="nofollow noreferrer"><code>WindowManager.removeViewImmediate()</code></a> contains a warning (emphasis mine):</p>
  
  <blockquote>
    <p>Special variation of <code>ViewManager.removeView(View)</code> that immediately invokes the given view hierarchy's <code>View.onDetachedFromWindow()</code> methods before returning. <strong>This is not for normal applications; using it correctly requires great care.</strong></p>
  </blockquote>
  
  <p>I'm curious what exactly "great care" means here. Presumably this is an indication that I shouldn't be calling this method unless I know how to handle all of the side-effects of using it... but I don't even know what those side-effects might be.</p>
  
  <hr>
  
  <p>Consider the following Activity, which I've made as vanilla as possible. There's no omitted code (other than imports), and the Activity's theme is just a vanilla AppCompat theme:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      private View overlay;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          getWindow().getDecorView().post(() -&gt; {
              overlay = new View(this);
              overlay.setBackgroundColor(Color.RED);
  
              WindowManager.LayoutParams params =
                      new WindowManager.LayoutParams(WindowManager.LayoutParams.FLAG_FULLSCREEN);
  
              getWindowManager().addView(overlay, params);
          });
      }
  
      @Override
      protected void onDestroy() {
          super.onDestroy();
          getWindowManager().removeView(overlay);
      }
  }
  </code></pre>
  
  <p>When I run my app, and rotate my device from portrait to landscape (or anything else that causes it to be destroyed and recreated), this appears in the logcat:</p>
  
  <blockquote>
  <pre><code>2018-10-09 13:58:02.162 11270-11270/com.example.stackoverflow E/WindowManager: android.view.WindowLeaked: Activity com.example.stackoverflow.MainActivity has leaked window android.view.View{e99861 V.ED..... ........ 0,0-1080,1920} that was originally added here
          at android.view.ViewRootImpl.&lt;init&gt;(ViewRootImpl.java:511)
          at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:346)
          at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
          at com.example.stackoverflow.MainActivity.lambda$onCreate$0(MainActivity.java:25)
  </code></pre>
  </blockquote>
  
  <p>However, if I change my <code>onDestroy()</code> method to use <code>removeViewImmedate()</code> instead, then this error never appears in my logs. I'm hesitant to do this, though, because I don't know what else could be affected by switching these calls. Just saying "it works" isn't enough to put my mind at ease.</p>