Skip to content

Commit 45554f8

Browse files
authored
Avoid throwing exceptions from native modules when the host activity … (#201)
* Avoid throwing exceptions from native modules when the host activity is not a FragmentActivity * Delete ReactFeatureFlags.js~d9a25c2d04f32259d711e38195689d376d4a06a7
1 parent a65c7af commit 45554f8

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

ReactAndroid/src/main/java/com/facebook/react/modules/datepicker/DatePickerDialogModule.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
package com.facebook.react.modules.datepicker;
99

10-
10+
import android.app.Activity;
1111
import android.app.DatePickerDialog.OnDateSetListener;
1212
import android.content.DialogInterface;
1313
import android.content.DialogInterface.OnDismissListener;
@@ -112,14 +112,16 @@ public void onDismiss(DialogInterface dialog) {
112112
*/
113113
@ReactMethod
114114
public void open(@Nullable final ReadableMap options, Promise promise) {
115-
FragmentActivity activity = (FragmentActivity) getCurrentActivity();
116-
if (activity == null) {
115+
Activity raw_activity = getCurrentActivity();
116+
if (raw_activity == null || !(raw_activity instanceof FragmentActivity)) {
117117
promise.reject(
118118
ERROR_NO_ACTIVITY,
119-
"Tried to open a DatePicker dialog while not attached to an Activity");
119+
"Tried to open a DatePicker dialog while not attached to a FragmentActivity");
120120
return;
121121
}
122122

123+
FragmentActivity activity = (FragmentActivity) raw_activity;
124+
123125
FragmentManager fragmentManager = activity.getSupportFragmentManager();
124126
DialogFragment oldFragment = (DialogFragment) fragmentManager.findFragmentByTag(FRAGMENT_TAG);
125127
if (oldFragment != null) {

ReactAndroid/src/main/java/com/facebook/react/modules/dialog/DialogModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public void run() {
239239
*/
240240
private @Nullable FragmentManagerHelper getFragmentManagerHelper() {
241241
Activity activity = getCurrentActivity();
242-
if (activity == null) {
242+
if (activity == null || !(activity instanceof FragmentActivity)) {
243243
return null;
244244
}
245245
return new FragmentManagerHelper(((FragmentActivity) activity).getSupportFragmentManager());

ReactAndroid/src/main/java/com/facebook/react/modules/timepicker/TimePickerDialogModule.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
package com.facebook.react.modules.timepicker;
99

10+
import android.app.Activity;
1011
import android.app.TimePickerDialog.OnTimeSetListener;
1112
import android.content.DialogInterface;
1213
import android.content.DialogInterface.OnDismissListener;
@@ -92,13 +93,16 @@ public void onDismiss(DialogInterface dialog) {
9293
@ReactMethod
9394
public void open(@Nullable final ReadableMap options, Promise promise) {
9495

95-
FragmentActivity activity = (FragmentActivity) getCurrentActivity();
96-
if (activity == null) {
96+
Activity raw_activity = getCurrentActivity();
97+
if (raw_activity == null || !(raw_activity instanceof FragmentActivity)) {
9798
promise.reject(
98-
ERROR_NO_ACTIVITY,
99-
"Tried to open a TimePicker dialog while not attached to an Activity");
99+
ERROR_NO_ACTIVITY,
100+
"Tried to open a DatePicker dialog while not attached to a FragmentActivity");
100101
return;
101102
}
103+
104+
FragmentActivity activity = (FragmentActivity) raw_activity;
105+
102106
// We want to support both android.app.Activity and the pre-Honeycomb FragmentActivity
103107
// (for apps that use it for legacy reasons). This unfortunately leads to some code duplication.
104108
FragmentManager fragmentManager = activity.getSupportFragmentManager();

0 commit comments

Comments
 (0)