Keyboard closing automatically in Android PhoneGap app

While building a phonegap app I encountered an issue with my Android build.  When I would select a text input and trigger the opening of the keyboard it would quickly close before I was able to type.  This issue appeared following my addition of an AdMob Plugin so I assumed it was something directly related to the ads.  Due to the high refresh rate I had set (for testing) I wondered if the ads were responsible for causing the keyboard being forced shut.   After spending hours researching ads and if their existence was ever correlated to Android keyboard issues I was stumped.  I dug deeper into the code source assessing every method triggered by the Ad Plugin  in the java and JavaScript code.  After removing much of the code I deemed “non essential” to keep the ads functioning, I came to shocking revelation.  The keyboard wasn’t actually getting forced closed anymore and it was all due to one single line.

webView.loadUrl("javascript:cordova.fireDocumentEvent('onAdChanged');");

This method was the culprit and all do to the use of “loadUrl” on the webview that housed the application.  Apparently according to the android documentation if loadURL is called it temporarily adjusts focus on the webview and as a result the keyboard thinks you’ve moved away from the TextInput and closes.  Since this code existed within the plugin as a request to notify my JavaScript of an ad changing it was simple enough to remove without causing any harm, since I don’t really care when it updates.  I will attempt to dig in further and see if I can devise a solution that allows me to contact JavaScript without disrupting keyboard functions.

There are no comments.

Leave a Reply