Skip to content

Commit b60fb21

Browse files
authored
fix: KeyboardToolbar in Modal on Android (#590)
## 📜 Description Fixed a problem of non-working `KeyboardToolbar` functionality inside of `Modal` on Android. ## 💡 Motivation and Context The problem comes from the fact, that `Modal` on Android lives in their own `window` hierarchy and as a result `activity?.currentFocus` is always `null` (`currentFocus` is `null`). A similar fix was added to iOS in this [commit](22babd2). On iOS the problem was in fact that we can not retrieve focused input if keyboard is dismissed (unlike Android, because on Android the view still in focus and carret is blinking after even keyboard dismissal). So on Android it was harder to notice that bug (especially when `Modal`s weren't officially supported). So the fix with replacing `.currentFocus` with `FocusedInputHolder.get()` seems to be pretty reliable, because our `FocusedInputHolder` will always hold a reference to currently focused input (doesn't matter which window contains that input, because we attach `OnGlobalFocusChangeListener` to every window). ## 📢 Changelog <!-- High level overview of important changes --> <!-- For example: fixed status bar manipulation; added new types declarations; --> <!-- If your changes don't affect one of platform/language below - then remove this platform/language --> ### Android - replace `activity?.currentFocus` with `FocusedInputHolder.get()`; ## 🤔 How Has This Been Tested? Tested manually on Pixel 3A (API 33, emulator). ## 📸 Screenshots (if appropriate): |Before|After| |-------|-----| |<video src="https://github.com/user-attachments/assets/f9390eed-f5cb-4e3d-840a-716399527b0b">|<video src="https://github.com/user-attachments/assets/5232d81f-8020-4e9f-a8d1-d145d33d71f1">| ## 📝 Checklist - [x] CI successfully passed - [x] I added new mocks and corresponding unit-tests if library API was changed
1 parent bc8d3b7 commit b60fb21

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

android/src/main/java/com/reactnativekeyboardcontroller/modules/KeyboardControllerModuleImpl.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ class KeyboardControllerModuleImpl(private val mReactContext: ReactApplicationCo
2222

2323
fun dismiss() {
2424
val activity = mReactContext.currentActivity
25-
val view: View? = activity?.currentFocus
25+
val view: View? = FocusedInputHolder.get()
2626

2727
if (view != null) {
28-
val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager
28+
val imm = activity?.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager
2929
imm?.hideSoftInputFromWindow(view.windowToken, 0)
3030
}
3131
}
@@ -35,8 +35,7 @@ class KeyboardControllerModuleImpl(private val mReactContext: ReactApplicationCo
3535
return FocusedInputHolder.focus()
3636
}
3737

38-
val activity = mReactContext.currentActivity
39-
val view: View? = activity?.currentFocus
38+
val view: View? = FocusedInputHolder.get()
4039

4140
if (view != null) {
4241
ViewHierarchyNavigator.setFocusTo(direction, view)

0 commit comments

Comments
 (0)