title: Handling Back button in custom View
body: <p>In a word game for Android with DrawerLayout the left and right navigation drawers are closed by the following code <strong>in the MainActivity</strong>:</p>
  
  <pre><code>@Override
  public void onBackPressed() {
      if (mDrawerLayout.isDrawerOpen(mLeftDrawer)) {
          mDrawerLayout.closeDrawer(mLeftDrawer);
      } else if (mDrawerLayout.isDrawerOpen(mRightDrawer)) {
          mDrawerLayout.closeDrawer(mRightDrawer);
      } else {
          super.onBackPressed();
      }
  }
  </code></pre>
  
  <p>This works well (the left screenshot) -</p>
  
  <p><a href="https://i.stack.imgur.com/sjeAj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sjeAj.png" alt="app screenshot"></a></p>
  
  <p>However I am also trying to detect Back button press <strong>in my custom View</strong> (which overrides <code>onDraw</code> method to draw scrollable game board and letter tiles) by the following methods - (to allow app user cancelling tile swapping or recalling unplayed tiles from the game board) - but none of them is called:</p>
  
  <pre><code>@Override
  public boolean dispatchKeyEventPreIme(KeyEvent event) {
      if (mSwapping &amp;&amp; event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
          cancelSwapLetters();
          return true;
      }
  
      return super.dispatchKeyEventPreIme(event);
  }
  
  @Override
  public boolean onKeyPreIme(int keyCode, KeyEvent event) {
      if (mSwapping &amp;&amp; keyCode == KeyEvent.KEYCODE_BACK) {
          cancelSwapLetters();
          return true;
      }
  
      return super.onKeyPreIme(keyCode, event);
  }
  
  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
      if (mSwapping &amp;&amp; keyCode == KeyEvent.KEYCODE_BACK) {
          cancelSwapLetters();
          return true;
      }
  
      return super.onKeyDown(keyCode, event);
  }
  </code></pre>
  
  <p>I have put breakpoints in the above methods, but none of them are hit.</p>
  
  <p>Please advise me, how to solve this problem.</p>