From aac95b409b38200c3e2a357c5faf77cf8273cb32 Mon Sep 17 00:00:00 2001 From: Oleksandr Melnykov Date: Thu, 30 May 2019 10:32:51 -0700 Subject: [PATCH] Fix switch being stuck in intermidiate state on Android Summary: This diff fixes the bug of the switch component on Android being stuck in the middle when a user releases their finger whily dragging the thumb. When a user releases their finger while dragging the thumb, `setChecked` will be called and if `mAllowChange` is set to false, `super.setChecked` is never called. The supper method will actually make sure the thumb will be animated to the correct edge. Without calling the super method, the thumb might stay in the middle of the switch where a user released their finger. The fix had to be applied both to ReactSwitch and FbReactSwitchCompat. One more fix had to be made to FbReactSwitchCompat since D5884661 was applied to ReactSwitch, but not to FbReactSwitchCompat: if (mAllowChange && **isChecked() != checked**) { ... } Reviewed By: mdvacca Differential Revision: D15535611 fbshipit-source-id: 22ca1fe3fa993ae65cbd677bfae2208a02c368d4 --- .../com/facebook/react/views/switchview/ReactSwitch.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/switchview/ReactSwitch.java b/ReactAndroid/src/main/java/com/facebook/react/views/switchview/ReactSwitch.java index fc716387770..dbbffb4bd78 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/switchview/ReactSwitch.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/switchview/ReactSwitch.java @@ -37,6 +37,11 @@ import javax.annotation.Nullable; mAllowChange = false; super.setChecked(checked); setTrackColor(checked); + } else { + // Even if mAllowChange is set to false or the checked value hasn't changed, we still must + // call the super method, since it will make sure the thumb is moved back to the correct edge. + // Without calling the super method, the thumb might stuck in the middle of the switch. + super.setChecked(isChecked()); } }