title: Android keyboard doesn't close
body: <p>I've been developing a custom keyboard for Android, and I'm experiencing some weird behaviour when changing keyboards...
  On the first run (either after being build from Android Studio or distributed via Fabric), the keyboard will not close, when I use:</p>
  
  <pre><code>InputMethodManager imeManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
  imeManager.switchToNextInputMethod(getToken(), false /* onlyCurrentIme */);
  </code></pre>
  
  <p>It will open the next input method behind my keyboard... But my keyboard will be forced upon on top of the other keyboard... It will even stay there, if you go to the home screen... But as soon as the device has been restarted, the keyboard changes without any issues... I've been reading up on this, and I've seen there was some issues with keyboards in previous versions of Android, are there still any?</p>
  
  <p>Below you can see the relevant code:</p>
  
  <pre><code>public class CustomKeyboard extends InputMethodService implements KeyboardView.OnKeyboardActionListener {
  
      private KeyboardView kv;
      private Keyboard keyboard;
  
      public final static int CodeRowFourGlobe = 55041;
  
      @Override
      public View onCreateInputView() {
          kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard, null);
          keyboard = new Keyboard(this, R.xml.custom_keyboard);
          kv.setPreviewEnabled(false);
          kv.setKeyboard(keyboard);
          kv.setOnKeyboardActionListener(this);
          return kv;
      }
  
      private IBinder getToken() {
          final Dialog dialog = getWindow();
          if (dialog == null) {
              return null;
          }
          final Window window = dialog.getWindow();
          if (window == null) {
              return null;
          }
          return window.getAttributes().token;
      }
  
      @Override
      public void onPress(int primaryCode) {
  
      }
  
      @Override
      public void onRelease(int primaryCode) {
  
      }
  
      @TargetApi(Build.VERSION_CODES.LOLLIPOP)
      @Override
      public void onKey(int primaryCode, int[] keyCodes) {
          InputConnection ic = getCurrentInputConnection();
  
          if (primaryCode == Keyboard.KEYCODE_DELETE) {
              ic.deleteSurroundingText(1, 0);
          } else if (primaryCode == CodeRowFourGlobe) {
              InputMethodManager imeManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
              imeManager.switchToNextInputMethod(getToken(), false /* onlyCurrentIme */);
          } else {
              /* not relevant */
          }
      }
  
      @Override
      public void onText(CharSequence text) {
  
      }
  
      @Override
      public void swipeLeft() {
  
      }
  
      @Override
      public void swipeRight() {
  
      }
  
      @Override
      public void swipeDown() {
  
      }
  
      @Override
      public void swipeUp() {
  
      }
  }
  </code></pre>
  
  <p>Do you have any idea if it's a problem with how I'm changing the keyboard or a known Android issue?</p>