Skip to content

Commit 057ac4e

Browse files
joevilchesfacebook-github-bot
authored andcommitted
Fix issue where text inputs cannot blur on <= Android 8.1
Summary: We got facebook#51072 (comment) which demonstrates that if we have 2 text inputs on a screen we cannot blur them. If you try to blur any, focus jumps to the first one. This seems to be a bug with Android's `clearFocus` per https://developer.android.com/reference/android/view/View#clearFocus(), this behavior is intended when we are not in touch mode, yet it happens regardless of what mode we are in on this version. I modified this a bit to swallow `requestFocus` calls if we are in touch mode. This should be fine as no JS focus calls will go through this path. On hardware keyboard focus and focus from `clearFocus` Changelog: [Android][Bugfix] - Fix bug where focus would jump to top text input upon clearing a separate text input. Differential Revision: D74678847
1 parent 34e9cba commit 057ac4e

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

packages/react-native/ReactAndroid/api/ReactAndroid.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6553,6 +6553,7 @@ public class com/facebook/react/views/textinput/ReactEditText : androidx/appcomp
65536553
public fun onTextContextMenuItem (I)Z
65546554
public fun onTouchEvent (Landroid/view/MotionEvent;)Z
65556555
public fun removeTextChangedListener (Landroid/text/TextWatcher;)V
6556+
public fun requestFocus (ILandroid/graphics/Rect;)Z
65566557
public final fun requestFocusFromJS ()V
65576558
public final fun setAllowFontScaling (Z)V
65586559
public final fun setAutoFocus (Z)V

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.kt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,9 +364,23 @@ public open class ReactEditText public constructor(context: Context) : AppCompat
364364
hideSoftKeyboard()
365365
}
366366

367+
override fun requestFocus(direction: Int, previouslyFocusedRect: Rect?): Boolean {
368+
// On some older versions of Android there is a bug where `clearFocus` will try to focus the
369+
// first focusable View in the hierarchy after clearing focus. This is intended behavior, but
370+
// only if you are not in touch mode per
371+
// https://developer.android.com/reference/android/view/View#clearFocus(), yet this happens in
372+
// both. Therefore, we are swallowing Android-based focus calls if we are in touch mode.
373+
// If we are not in touch mode (using a hardware keyboard) then we will allow this to happen.
374+
// Note this only happens for Android-origin focus calls, as opposed to JS-origin (like tapping)
375+
// since those go through `requestFocusProgrammatically`
376+
if (isInTouchMode) {
377+
return isFocused
378+
}
379+
return super.requestFocus(direction, previouslyFocusedRect)
380+
}
381+
367382
// For cases like autoFocus, or ref.focus() where we request focus programmatically and not
368-
// through
369-
// interacting with the EditText directly (like clicking on it). We cannot use stock
383+
// through interacting with the EditText directly (like clicking on it). We cannot use stock
370384
// requestFocus() because it will not pop up the soft keyboard, only clicking the input will do
371385
// that. This method will eventually replace requestFocusInternal()
372386
private fun requestFocusProgrammatically(): Boolean {

0 commit comments

Comments
 (0)