The keyboard app shouldn't have to have anything to do with switching layouts when the language changes.

When the user changes the language, the settings app will enable a new keyboard layout.  And the keyboard app will be notified of that change.

I think you should just have the keyboard app switch to whatever keyboard layout is most recently enabled.

Also, if you do it this way, there might be a race condition... The keyboard will proabably be notified of the language change before it is notified of the keyboard layout enabled change. It could enable a new keyboard before that keyboard is added to the enabledKeyboardNames[] array. I don't know if that will cause bugs, but it makes me nervous.