Skip to content

[0.79] Fix broken focus behavior for TextInput in older Android versions (< 9) #51964

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<b263bdcbc1258f7d5c56e69732ba9076>>
* @generated SignedSource<<fa641112b3a8888ba2b24cf8829e9382>>
*/

/**
Expand Down Expand Up @@ -250,12 +250,6 @@ public object ReactNativeFeatureFlags {
@JvmStatic
public fun useAlwaysAvailableJSErrorHandling(): Boolean = accessor.useAlwaysAvailableJSErrorHandling()

/**
* If true, focusing in ReactEditText will mainly use stock Android requestFocus() behavior. If false it will use legacy custom focus behavior.
*/
@JvmStatic
public fun useEditTextStockAndroidFocusBehavior(): Boolean = accessor.useEditTextStockAndroidFocusBehavior()

/**
* Should this application enable the Fabric Interop Layer for Android? If yes, the application will behave so that it can accept non-Fabric components and render them on Fabric. This toggle is controlling extra logic such as custom event dispatching that are needed for the Fabric Interop Layer to work correctly.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<0496ecf3d1e5d8a2e6d4d594aca806d0>>
* @generated SignedSource<<9ecb711480b7d6c22bac380c28d035bc>>
*/

/**
Expand Down Expand Up @@ -57,7 +57,6 @@ internal class ReactNativeFeatureFlagsCxxAccessor : ReactNativeFeatureFlagsAcces
private var traceTurboModulePromiseRejectionsOnAndroidCache: Boolean? = null
private var updateRuntimeShadowNodeReferencesOnCommitCache: Boolean? = null
private var useAlwaysAvailableJSErrorHandlingCache: Boolean? = null
private var useEditTextStockAndroidFocusBehaviorCache: Boolean? = null
private var useFabricInteropCache: Boolean? = null
private var useNativeViewConfigsInBridgelessModeCache: Boolean? = null
private var useOptimizedEventBatchingOnAndroidCache: Boolean? = null
Expand Down Expand Up @@ -399,15 +398,6 @@ internal class ReactNativeFeatureFlagsCxxAccessor : ReactNativeFeatureFlagsAcces
return cached
}

override fun useEditTextStockAndroidFocusBehavior(): Boolean {
var cached = useEditTextStockAndroidFocusBehaviorCache
if (cached == null) {
cached = ReactNativeFeatureFlagsCxxInterop.useEditTextStockAndroidFocusBehavior()
useEditTextStockAndroidFocusBehaviorCache = cached
}
return cached
}

override fun useFabricInterop(): Boolean {
var cached = useFabricInteropCache
if (cached == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<c4f3b0cee8b9b4b9cebb589801e1dd15>>
* @generated SignedSource<<2151e5ec5d04924e742f37b527dc23b9>>
*/

/**
Expand Down Expand Up @@ -102,8 +102,6 @@ public object ReactNativeFeatureFlagsCxxInterop {

@DoNotStrip @JvmStatic public external fun useAlwaysAvailableJSErrorHandling(): Boolean

@DoNotStrip @JvmStatic public external fun useEditTextStockAndroidFocusBehavior(): Boolean

@DoNotStrip @JvmStatic public external fun useFabricInterop(): Boolean

@DoNotStrip @JvmStatic public external fun useNativeViewConfigsInBridgelessMode(): Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<a8900217ae0385947b619c8fa0834942>>
* @generated SignedSource<<56f86a3a0c0bbf453cf45a0db541ef54>>
*/

/**
Expand Down Expand Up @@ -97,8 +97,6 @@ public open class ReactNativeFeatureFlagsDefaults : ReactNativeFeatureFlagsProvi

override fun useAlwaysAvailableJSErrorHandling(): Boolean = false

override fun useEditTextStockAndroidFocusBehavior(): Boolean = true

override fun useFabricInterop(): Boolean = false

override fun useNativeViewConfigsInBridgelessMode(): Boolean = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<8f5180a0ef154c083ac38d28e650ee11>>
* @generated SignedSource<<5b016fd6298477856116736e37c37c6f>>
*/

/**
Expand Down Expand Up @@ -61,7 +61,6 @@ internal class ReactNativeFeatureFlagsLocalAccessor : ReactNativeFeatureFlagsAcc
private var traceTurboModulePromiseRejectionsOnAndroidCache: Boolean? = null
private var updateRuntimeShadowNodeReferencesOnCommitCache: Boolean? = null
private var useAlwaysAvailableJSErrorHandlingCache: Boolean? = null
private var useEditTextStockAndroidFocusBehaviorCache: Boolean? = null
private var useFabricInteropCache: Boolean? = null
private var useNativeViewConfigsInBridgelessModeCache: Boolean? = null
private var useOptimizedEventBatchingOnAndroidCache: Boolean? = null
Expand Down Expand Up @@ -440,16 +439,6 @@ internal class ReactNativeFeatureFlagsLocalAccessor : ReactNativeFeatureFlagsAcc
return cached
}

override fun useEditTextStockAndroidFocusBehavior(): Boolean {
var cached = useEditTextStockAndroidFocusBehaviorCache
if (cached == null) {
cached = currentProvider.useEditTextStockAndroidFocusBehavior()
accessedFeatureFlags.add("useEditTextStockAndroidFocusBehavior")
useEditTextStockAndroidFocusBehaviorCache = cached
}
return cached
}

override fun useFabricInterop(): Boolean {
var cached = useFabricInteropCache
if (cached == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<33571f99b1f78fbc62cecfca5f8351fa>>
* @generated SignedSource<<b4d6157922f6182dd588d5ae5b54ead9>>
*/

/**
Expand Down Expand Up @@ -97,8 +97,6 @@ public interface ReactNativeFeatureFlagsProvider {

@DoNotStrip public fun useAlwaysAvailableJSErrorHandling(): Boolean

@DoNotStrip public fun useEditTextStockAndroidFocusBehavior(): Boolean

@DoNotStrip public fun useFabricInterop(): Boolean

@DoNotStrip public fun useNativeViewConfigsInBridgelessMode(): Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityNodeInfo;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
Expand Down Expand Up @@ -146,9 +147,6 @@ public class ReactEditText extends AppCompatEditText {

public ReactEditText(Context context) {
super(context);
if (!ReactNativeFeatureFlags.useEditTextStockAndroidFocusBehavior()) {
setFocusableInTouchMode(false);
}

mInputMethodManager =
(InputMethodManager)
Expand Down Expand Up @@ -191,9 +189,7 @@ public boolean performAccessibilityAction(View host, int action, Bundle args) {
// selection on accessibility click to undo that.
setSelection(length);
}
return ReactNativeFeatureFlags.useEditTextStockAndroidFocusBehavior()
? requestFocusProgramatically()
: requestFocusInternal();
return requestFocusProgramatically();
}
return super.performAccessibilityAction(host, action, args);
}
Expand Down Expand Up @@ -341,29 +337,28 @@ public boolean onTextContextMenuItem(int id) {
return super.onTextContextMenuItem(id);
}

@Override
public void clearFocus() {
boolean useStockFocusBehavior = ReactNativeFeatureFlags.useEditTextStockAndroidFocusBehavior();
if (!useStockFocusBehavior) {
setFocusableInTouchMode(false);
public void clearFocusAndMaybeRefocus() {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P || !isInTouchMode()) {
super.clearFocus();
} else {
// Avoid refocusing to a new view on old versions of Android by default
// by preventing `requestFocus()` on the rootView from moving focus to any child.
// https://cs.android.com/android/_/android/platform/frameworks/base/+/bdc66cb5a0ef513f4306edf9156cc978b08e06e4
ViewGroup rootViewGroup = (ViewGroup)getRootView();
int oldDescendantFocusability = rootViewGroup.getDescendantFocusability();
rootViewGroup.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
super.clearFocus();
rootViewGroup.setDescendantFocusability(oldDescendantFocusability);
}
super.clearFocus();

hideSoftKeyboard();
}

@Override
public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
// This is a no-op so that when the OS calls requestFocus(), nothing will happen. ReactEditText
// is a controlled component, which means its focus is controlled by JS, with two exceptions:
// autofocus when it's attached to the window, and responding to accessibility events. In both
// of these cases, we call requestFocusInternal() directly.
return ReactNativeFeatureFlags.useEditTextStockAndroidFocusBehavior()
? super.requestFocus(direction, previouslyFocusedRect)
: isFocused();
/* package */ void clearFocusFromJS() {
clearFocusAndMaybeRefocus();
}

private boolean requestFocusInternal() {
setFocusableInTouchMode(true);
// We must explicitly call this method on the super class; if we call requestFocus() without
// any arguments, it will call into the overridden requestFocus(int, Rect) above, which no-ops.
boolean focused = super.requestFocus(View.FOCUS_DOWN, null);
Expand Down Expand Up @@ -656,15 +651,7 @@ public void maybeUpdateTypeface() {

// VisibleForTesting from {@link TextInputEventsTestCase}.
public void requestFocusFromJS() {
if (ReactNativeFeatureFlags.useEditTextStockAndroidFocusBehavior()) {
requestFocusProgramatically();
} else {
requestFocusInternal();
}
}

/* package */ void clearFocusFromJS() {
clearFocus();
requestFocusProgramatically();
}

// VisibleForTesting from {@link TextInputEventsTestCase}.
Expand Down Expand Up @@ -1107,11 +1094,7 @@ public void onAttachedToWindow() {
}

if (mAutoFocus && !mDidAttachToWindow) {
if (ReactNativeFeatureFlags.useEditTextStockAndroidFocusBehavior()) {
requestFocusProgramatically();
} else {
requestFocusInternal();
}
requestFocusProgramatically();
}

mDidAttachToWindow = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,7 @@ protected void addEventEmitters(
}

if (shouldBlur) {
editText.clearFocus();
editText.clearFocusAndMaybeRefocus();
}

// Prevent default behavior except when we want it to insert a newline.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<83b23039ed9fff5109ff3b532648baac>>
* @generated SignedSource<<cf0734c38bab916ecaf361bc557b8802>>
*/

/**
Expand Down Expand Up @@ -261,12 +261,6 @@ class ReactNativeFeatureFlagsProviderHolder
return method(javaProvider_);
}

bool useEditTextStockAndroidFocusBehavior() override {
static const auto method =
getReactNativeFeatureFlagsProviderJavaClass()->getMethod<jboolean()>("useEditTextStockAndroidFocusBehavior");
return method(javaProvider_);
}

bool useFabricInterop() override {
static const auto method =
getReactNativeFeatureFlagsProviderJavaClass()->getMethod<jboolean()>("useFabricInterop");
Expand Down Expand Up @@ -498,11 +492,6 @@ bool JReactNativeFeatureFlagsCxxInterop::useAlwaysAvailableJSErrorHandling(
return ReactNativeFeatureFlags::useAlwaysAvailableJSErrorHandling();
}

bool JReactNativeFeatureFlagsCxxInterop::useEditTextStockAndroidFocusBehavior(
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop> /*unused*/) {
return ReactNativeFeatureFlags::useEditTextStockAndroidFocusBehavior();
}

bool JReactNativeFeatureFlagsCxxInterop::useFabricInterop(
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop> /*unused*/) {
return ReactNativeFeatureFlags::useFabricInterop();
Expand Down Expand Up @@ -680,9 +669,6 @@ void JReactNativeFeatureFlagsCxxInterop::registerNatives() {
makeNativeMethod(
"useAlwaysAvailableJSErrorHandling",
JReactNativeFeatureFlagsCxxInterop::useAlwaysAvailableJSErrorHandling),
makeNativeMethod(
"useEditTextStockAndroidFocusBehavior",
JReactNativeFeatureFlagsCxxInterop::useEditTextStockAndroidFocusBehavior),
makeNativeMethod(
"useFabricInterop",
JReactNativeFeatureFlagsCxxInterop::useFabricInterop),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<efb0288fd19fb35e4582522c835301b4>>
* @generated SignedSource<<f295d109e9a81ce2d2040a5fc89e9ada>>
*/

/**
Expand Down Expand Up @@ -141,9 +141,6 @@ class JReactNativeFeatureFlagsCxxInterop
static bool useAlwaysAvailableJSErrorHandling(
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop>);

static bool useEditTextStockAndroidFocusBehavior(
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop>);

static bool useFabricInterop(
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop>);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<661a4193f9ba7af0c963bc13751deb15>>
* @generated SignedSource<<1c90106ded5dc5f08cdec4dede695341>>
*/

/**
Expand Down Expand Up @@ -174,10 +174,6 @@ bool ReactNativeFeatureFlags::useAlwaysAvailableJSErrorHandling() {
return getAccessor().useAlwaysAvailableJSErrorHandling();
}

bool ReactNativeFeatureFlags::useEditTextStockAndroidFocusBehavior() {
return getAccessor().useEditTextStockAndroidFocusBehavior();
}

bool ReactNativeFeatureFlags::useFabricInterop() {
return getAccessor().useFabricInterop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<53e965423f2957b37e0470c607cc45a4>>
* @generated SignedSource<<ce2f5895234f1cfb30374c72db54ccce>>
*/

/**
Expand Down Expand Up @@ -224,11 +224,6 @@ class ReactNativeFeatureFlags {
*/
RN_EXPORT static bool useAlwaysAvailableJSErrorHandling();

/**
* If true, focusing in ReactEditText will mainly use stock Android requestFocus() behavior. If false it will use legacy custom focus behavior.
*/
RN_EXPORT static bool useEditTextStockAndroidFocusBehavior();

/**
* Should this application enable the Fabric Interop Layer for Android? If yes, the application will behave so that it can accept non-Fabric components and render them on Fabric. This toggle is controlling extra logic such as custom event dispatching that are needed for the Fabric Interop Layer to work correctly.
*/
Expand Down
Loading
Loading