iPhone: Why does the keyboard language switch randomly?
What you are observing is not really random. It is programmatically possible to remember the last used keyboard language, from among the set of languages chosen by user under Settings app → General → Keyboard → Keyboards, on a per text input field basis. This is employed by apps which present multiple text entry fields (such as messaging apps) to automatically choose the last used or the most relevant language.

Let me explain it with the help of an example. Suppose in a messaging app, you prefer conversing with some users in Simplified Chinese and with some in English. It's possible to program the app so that a text input field remembers the last used keyboard language. This way, the last chosen language is automatically selected for you. However, this doesn't work with the Emoji keyboard. This approach can be taken to aid user convenience.

If you are interested in learning about the programming aspect of how this can be achieved, refer to this answer on Stack Overflow:


https://stackoverflow.com/a/23403247/6995250