App Closes if Bask-Button is pressed while focussing input-element
I just encountered a interesting Bug within a Cordova/Phonegap App:
Everytime a user enteres something into a input-field, the softkeyboard will show up. But if the user hides the keyboard and than hit the Android-Back-Button the application closed.
No errormessage on screen, nothing strange in the log. Just a clear shutdown of the App.
If the user hides the keyboard, unfocus the input-field and hits back, the app worked as designed.
After some time I found the problem:
The Bback-Button will end the DroidGap Activity, that closes the app.
This ISSUE at the Codova Issue-Tracker described a similar problem:
Cordova Issue Tracker
I found a simple solution there. Just override some native Key-Events in you DroidGap-Activity:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
LOG.d(TAG, "KeyDown has been triggered on the view"+keyCode);
//If volumedown key
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
// only override default behaviour is event bound
LOG.d(TAG, "Down Key >Hit");
this.loadUrl("javascript:cordova.fireDocumentEvent('volumedownbutton');");
return true;
}
// If volumeup key
else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
LOG.d(TAG, "Up Key Hit");
this.loadUrl("javascript:cordova.fireDocumentEvent('volumeupbutton');");
return true;
} else {
//return super.onKeyDown(keyCode, event);
}
//return super.onKeyDown(keyCode, event);
return true;
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event){
LOG.d(TAG, "KeyUp has been triggered on the view"+keyCode);
// If back key
if (keyCode == KeyEvent.KEYCODE_BACK) {
this.loadUrl("javascript:cordova.fireDocumentEvent('backbutton');");
return true;
}
// Legacy
else if (keyCode == KeyEvent.KEYCODE_MENU) {
this.loadUrl("javascript:cordova.fireDocumentEvent('menubutton');");
return true;
}
// If search key
else if (keyCode == KeyEvent.KEYCODE_SEARCH) {
this.loadUrl("javascript:cordova.fireDocumentEvent('searchbutton');");
return true;
}
return false;
}