Skip to content

Commit 58437cd

Browse files
vonovakfacebook-github-bot
authored andcommitted
fix checkbox casting crash in old android versions (#23281)
Summary: fixes casting on older android versions reported in #22885 [Android] [Fixed] - fix casting crash in android checkbox Pull Request resolved: #23281 Differential Revision: D13941776 Pulled By: cpojer fbshipit-source-id: ff3695f64d3399790a03b02d5b1363cacc655336
1 parent 9ccde37 commit 58437cd

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

ReactAndroid/src/main/java/com/facebook/react/views/checkbox/ReactCheckBoxManager.java

+14-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
*/
77
package com.facebook.react.views.checkbox;
88

9+
import android.content.Context;
10+
import android.support.v7.widget.TintContextWrapper;
911
import android.widget.CompoundButton;
1012
import com.facebook.react.bridge.ReactContext;
1113
import com.facebook.react.uimanager.SimpleViewManager;
1214
import com.facebook.react.uimanager.ThemedReactContext;
1315
import com.facebook.react.uimanager.UIManagerModule;
1416
import com.facebook.react.uimanager.ViewProps;
1517
import com.facebook.react.uimanager.annotations.ReactProp;
16-
import com.facebook.react.uimanager.events.EventDispatcher;
1718

1819
/** View manager for {@link ReactCheckBox} components. */
1920
public class ReactCheckBoxManager extends SimpleViewManager<ReactCheckBox> {
@@ -24,11 +25,22 @@ public class ReactCheckBoxManager extends SimpleViewManager<ReactCheckBox> {
2425
new CompoundButton.OnCheckedChangeListener() {
2526
@Override
2627
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
27-
ReactContext reactContext = (ReactContext) buttonView.getContext();
28+
ReactContext reactContext = getReactContext(buttonView);
2829
reactContext
2930
.getNativeModule(UIManagerModule.class).getEventDispatcher()
3031
.dispatchEvent(new ReactCheckBoxEvent(buttonView.getId(), isChecked));
3132
}
33+
34+
private ReactContext getReactContext(CompoundButton buttonView) {
35+
ReactContext reactContext;
36+
Context ctx = buttonView.getContext();
37+
if (ctx instanceof TintContextWrapper) {
38+
reactContext = (ReactContext) ((TintContextWrapper) ctx).getBaseContext();
39+
} else {
40+
reactContext = (ReactContext) buttonView.getContext();
41+
}
42+
return reactContext;
43+
}
3244
};
3345

3446
@Override

0 commit comments

Comments
 (0)