title: Android Error IllegalArgumentException: Window type can not be changed after the window is added
body: <p>I'm trying to make custom lock screen. So, there I need not to allow user to press <em>Home Button</em>. In the begining I write </p>
  
  <pre><code>public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.lock_screen);
  
      getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                          |WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
  </code></pre>
  
  <p>Then I override <em>OnKeyDown</em></p>
  
  <pre><code> @Override
  public boolean onKeyDown(int keyCode, android.view.KeyEvent event) {
  
      if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)||(keyCode == KeyEvent.KEYCODE_POWER)||(keyCode == KeyEvent.KEYCODE_VOLUME_UP)||(keyCode == KeyEvent.KEYCODE_CAMERA)) {
          //this is where I can do my stuff
          return true; //because I handled the event
      }
      if((keyCode == KeyEvent.KEYCODE_HOME)){
  
          return true;
      }
  
      return false;
  
  }
  </code></pre>
  
  <p>Here I override <em>onAttacheToWindow</em></p>
  
  <pre><code> @Override
  public void onAttachedToWindow() {
      this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
      super.onAttachedToWindow();
  }
  </code></pre>
  
  <p>But it's giving me error <strong>IllegalArgumentException: Window type can not be changed after the window is added.</strong> Where is my mistake? </p>
  
  <p><strong>How can I handle Home Button?</strong></p>