Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InputMethodService lifecycle bug

I am writing my own InputMethodService and I want to detect basically when the keyboard pops up and down in order to start and stop doing stuff. I have the simplest `MyInput' class that does very little:

public class MyInput extends InputMethodService {
    private static final String TAG = "MyInput";

    @Override
    public View onCreateInputView() {
        Log.d(TAG, "onCreateInputView");
        LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        return inflater.inflate(R.layout.my_keyboard, null);
    }

    @Override
    public void onStartInput(EditorInfo attribute, boolean restarting) {
        super.onStartInput(EditorInfo attribute, boolean restarting);
        Log.d(TAG, "onStartInput restarting = " + restarting);
    }

    @Override
    public void onFinishInput() {
        super.onFinishInput();
        Log.d(TAG, "onFinishInput");
    }
}

My view pops up and down normally but in the log, I can see a very strange behavior. Every time the keyboard shows or hide, both functions are called; making it impossible for me to detect when it's actually showing or not.

/** Keyboard not showing, I press an TextView **/
D  onFinishInput
D  onStartInput restarting = false
/** Keyboard showing, I press back **/
D  onFinishInput
D  onStartInput restarting = false
/** Keyboard not showing **/

I don't understand why such a simple example doesn't work. Thanks for any help

like image 379
chopchop Avatar asked Nov 03 '25 00:11

chopchop


1 Answers

The official documentation of the IME Lifecycle is really lacking unfortunately. Through lots of debugging I created (for myself initially) a much better documentation of the IME Lifecycle:

IME Lifecycle

Regarding your initial question, if you want to know if your Keyboard is currently showing or not you shouldn't look at onStartInput, but you need to look at onStartInputView. Your Keyboard is visible in the calls between onStartInputView and onFinishInputView.

like image 178
Bernd Kampl Avatar answered Nov 05 '25 16:11

Bernd Kampl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!