Skip to content

Commit bbab2ab

Browse files
committed
Android: Improve back button handling when the keyboard is visible
1 parent 7d4f478 commit bbab2ab

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

src/glfm_android.c

+22-9
Original file line numberDiff line numberDiff line change
@@ -1039,14 +1039,16 @@ static uint32_t glfm__getUnicodeChar(GLFMPlatformData *platformData, jint keyCod
10391039
}
10401040

10411041
/*
1042-
* Move task to the back if it is root task. This make the back button have the same behavior
1043-
* as the home button.
1042+
* Hide the virtual keyboard if it is visible.
10441043
*
1045-
* Without this, when the user presses the back button, the loop in glfm__mainLoop() is exited, the
1046-
* OpenGL context is destroyed, and the main thread is destroyed. The glfm__mainLoop() function
1047-
* would be called again in the same process if the user returns to the app.
1044+
* Otherwise, if the current task is the root task, move the task to the back. This makes the back button have the same
1045+
* behavior as the home button.
10481046
*
1049-
* When this, when the app is in the background, the app will pause in the ALooper_pollAll() call.
1047+
* Without this, when the user presses the back button, the loop in glfm__mainLoop() is exited, the OpenGL context is
1048+
* destroyed, and the main thread is destroyed. The glfm__mainLoop() function would be called again in the same process
1049+
* if the user returns to the app.
1050+
*
1051+
* With this, when the app is in the background, the app will pause in the ALooper_pollAll() call.
10501052
*/
10511053
static bool glfm__handleBackButton(GLFMPlatformData *platformData) {
10521054
if (!platformData || !platformData->activity) {
@@ -1057,9 +1059,18 @@ static bool glfm__handleBackButton(GLFMPlatformData *platformData) {
10571059
return false;
10581060
}
10591061

1062+
if (platformData->keyboardVisible) {
1063+
glfmSetKeyboardVisible(platformData->display, false);
1064+
return true;
1065+
}
1066+
1067+
#if GLFM_HANDLE_BACK_BUTTON
10601068
jboolean handled = glfm__callJavaMethodWithArgs(jni, platformData->activity->clazz,
10611069
"moveTaskToBack", "(Z)Z", Boolean, false);
10621070
return !glfm__wasJavaExceptionThrown(jni) && handled;
1071+
#else
1072+
return false;
1073+
#endif
10631074
}
10641075

10651076
static bool glfm__onKeyEvent(GLFMPlatformData *platformData, AInputEvent *event) {
@@ -1250,11 +1261,9 @@ static bool glfm__onKeyEvent(GLFMPlatformData *platformData, AInputEvent *event)
12501261
}
12511262
}
12521263

1253-
#if GLFM_HANDLE_BACK_BUTTON
12541264
if (!handled && aAction == AKEY_EVENT_ACTION_UP && aKeyCode == AKEYCODE_BACK) {
12551265
handled = glfm__handleBackButton(platformData);
12561266
}
1257-
#endif
12581267

12591268
if (display->charFunc && (aAction == AKEY_EVENT_ACTION_DOWN || aAction == AKEY_EVENT_ACTION_MULTIPLE)) {
12601269
uint32_t unicode = glfm__getUnicodeChar(platformData, aKeyCode, aMetaState);
@@ -1337,9 +1346,13 @@ static bool glfm__onTouchEvent(GLFMPlatformData *platformData, AInputEvent *even
13371346
static void glfm__onInputEvent(GLFMPlatformData *platformData) {
13381347
AInputEvent *event = NULL;
13391348
while (AInputQueue_getEvent(platformData->inputQueue, &event) >= 0) {
1340-
if (AInputQueue_preDispatchEvent(platformData->inputQueue, event)) {
1349+
bool skipPreDispatch = (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_KEY &&
1350+
AKeyEvent_getKeyCode(event) == AKEYCODE_BACK);
1351+
1352+
if (!skipPreDispatch && AInputQueue_preDispatchEvent(platformData->inputQueue, event)) {
13411353
continue;
13421354
}
1355+
13431356
bool handled = false;
13441357
int32_t eventType = AInputEvent_getType(event);
13451358
if (eventType == AINPUT_EVENT_TYPE_KEY) {

0 commit comments

Comments
 (0)