0

I'm building an android soft keyboard and I can't seem to fix this bug - I have an Arabic and QWERTY keyboard and when I rotate my device on the QWERTY keyboard (or even Arabic shift), it's as if my program has "restarted" and it becomes the Arabic Keyboard without shift.

The onSaveInstanceState(Bundle savedInstanceState) does not work because my application does not extend Activity but InputMethodService.

I put the following in my android manifest

android:configChanges="keyboard|keyboardHidden|orientation"
android:windowSoftInputMode="stateUnchanged|adjustResize">

I tried using

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        Log.i(MYDEBUG, "Config Changed " + currentKeyboard.equals(qwerty));

    }

However, currentKeyboard.equals(qwerty)) always results to false and I made sure it was true just before the orientation change.

Any help would be much appreciated.

sometimes24
  • 355
  • 4
  • 15

2 Answers2

0

I think its the applications fault. If the application is restarting on orientation changes, then your input connection is being torn down and rebuilt to a new edit text. This means the keyboard would see it as a new connection and will start in the default state. To test this, write a test app that turns off restarts on configuration change and see if it still happens to keyboards in that app.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Sorry how do I stop the application from restarting? I thought adding the line of code in the manifest stops it from doing that. – sometimes24 Jul 14 '15 at 00:23
  • Adding it to the application's manifest does that. Adding it to the keyboard's doesn't. It would just stop the keyboard service from being restarted. – Gabe Sechan Jul 14 '15 at 00:23
  • Sorry you sort of lost me. I'm a little new in the field of android development. Do you think you can clarify what you said? Thank you – sometimes24 Jul 14 '15 at 00:33
-1

Alright, after mind boggling thinking I realized the issue was with the line of code super.onConfigurationChanged(newConfig);

When removed, my code would change orientation but my keyboard would not be resized. Since I love the re-sizing feature of the parent, I made an array that contains all the keyboards and after changing orientation, I would update the array so that the keyboard array would have the right sizes.

@Override
public void onConfigurationChanged(Configuration newConfig) {

    int currentKeyboard = 0;
    boolean isShifted = kv.isShifted();
    for (int i = 0; i < keyboard.length; i++) {
        if(kv.getKeyboard().equals(keyboard[i])){
            currentKeyboard = i;
            break;
        }
    }

    super.onConfigurationChanged(newConfig);

    initializeKeyboardArray();
    setKeyboard(keyboard[currentKeyboard]);
    kv.setShifted(isShifted);

}

private void initializeKeyboardArray(){
    keyboard = new Keyboard[7];
    keyboard[ARABIC] = arabic;
    keyboard[ARABIC_SHIFT] = arabicShift;
    keyboard[ARABIC_SYMBOLS] = arabicSymbols;
    keyboard[ARABIC_SYMBOLS_SHIFT] = arabicSymbolsShift;
    keyboard[QWERTY] = qwerty;
    keyboard[QWERTY_SYMBOLS] = qwertySymbols;
    keyboard[QWERTY_SYMBOLS_SHIFT] = qwertySymbolsShift;
}

Not sure if this is a roundabout way of solving the problem or if this is the right way so if anyone knows a better way - please let me know.

sometimes24
  • 355
  • 4
  • 15