title: How to use the back press to remove the view added by WindowManager?
body: <p>guys， I am developing an android AppLock Application, and I use WindowManager to add a gesture lock view.
  but I have a problem.
  back key press doesn't remove the view.
  And I can't rewrite the View to override the dispatchKeyEvent() function, as I use LayoutInflater to inflate my view. Just like that:</p>
  
  <pre><code>LayoutInflater layoutInflater = 
              (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  enterPassView = layoutInflater.inflate(R.layout.activity_enterpass, null);
  </code></pre>
  
  <p>Please help me to slove this problem!
  Thank you.</p>
  
  <hr>
  
  <p>Add in UTC+8 2014/12/31 18:20</p>
  
  <p><strong><em>hello, I am so happy as I use a simple way to solve my problem. I use two flag variable, one is viewFlag(when I call addView( ), viewFlag = true; and when I call removeView( ), viewFlag = false), the other one is lockedFlag(when the top App is locked by user add in database, lockedFlag = true, and when it's not locked, lockedFlag = false)
  So, if(lockedFlag == false &amp;&amp; viewFlag == true &amp;&amp; view != null)  {   //I send message to a handler to call removeView( )   }</em></strong></p>
  
  <hr>
  
  <pre><code>class EnterPassView extends View {
  
      public EnterPassView(Context context) {
          super(context);
      }
  
      @Override
      public boolean dispatchKeyEvent(KeyEvent event) {
          if(event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
              Message message = viewHandler.obtainMessage(0);
              viewHandler.sendMessage(message);
              return super.dispatchKeyEvent(event);
          } else if(event.getKeyCode() == KeyEvent.KEYCODE_HOME) {
              Message message = viewHandler.obtainMessage(0);
              viewHandler.sendMessage(message);
              return super.dispatchKeyEvent(event);
          } else if(event.getKeyCode() == KeyEvent.KEYCODE_MENU) {
              Message message = viewHandler.obtainMessage(0);
              viewHandler.sendMessage(message);
              return super.dispatchKeyEvent(event);
          }
          return super.dispatchKeyEvent(event);
      }
  
  }
  
  private void showGestureLockView() {
      params = new WindowManager.LayoutParams(
              WindowManager.LayoutParams.MATCH_PARENT, 
              WindowManager.LayoutParams.MATCH_PARENT, 
              WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, 
              WindowManager.LayoutParams.FLAG_FULLSCREEN | 
                  WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, 
              PixelFormat.RGBA_8888);
      params.gravity = (Gravity.CENTER);
  
      LayoutInflater layoutInflater = 
              (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      enterPassView = layoutInflater.inflate(R.layout.activity_enterpass, null);
  
      iv_icon = (ImageView) enterPassView.findViewById(R.id.enterpass_iv_icon);
      tv_text = (TextView) enterPassView.findViewById(R.id.enterpass_tv_text);
  
      animation = new TranslateAnimation(-20, 20, 0, 0);
      animation.setDuration(50);
      animation.setRepeatCount(2);
      animation.setRepeatMode(Animation.REVERSE);
  
      SharedPreferences sp = getSharedPreferences(AppConfig.APP_SP_NAME, MODE_PRIVATE);
      String savePass = sp.getString(AppConfig.GESTURE_PASS, "");
      gesturePassView = (GesturePassView) enterPassView.findViewById(R.id.enterpass_view_gpv);
      gesturePassView.setSavedPass(savePass);
      gesturePassView.setScene(0);
      gesturePassView.setOnGestureCompleteListener(new OnGestureCompleteListener() {
  
          @Override
          public void onGestureComplete(int result, String enterPassword) {
              if(result == 0) {
                  iv_icon.setAnimation(animation);
                  tv_text.setText(R.string.enterpass_lesspass);
              } else if(result == 1){
                  Message message = viewHandler.obtainMessage(0);
                  viewHandler.sendMessage(message);
                  Log.e("sendMessage", "pass is true want to remove view");
              } else if(result == -1){
                  iv_icon.setAnimation(animation);
                  tv_text.setText(R.string.enterpass_wrongpass);
              }
          }
      });
  
      if(windowsManager == null) {
          windowsManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
      }
      if(windowsManager != null &amp;&amp; enterPassView != null) {
          Message message = viewHandler.obtainMessage(1);
          viewHandler.sendMessage(message);
          Log.e("sendMessage", "want to addView");
      }
  
  }
  
  Handler viewHandler = new Handler() {
  
      @Override
      public void handleMessage(Message message) {
          int flag = message.what;
          if(flag == 1) {
              windowsManager.addView(enterPassView, params);
              Log.e("sendMessage", "addView");
          } else if(flag == 0) {
              windowsManager.removeView(enterPassView);
              enterPassView = null;
              Log.e("sendMessage", "removeView");
          }
  
      }
  
  };
  </code></pre>