Skip to content

Commit 7cfabf4

Browse files
mganandrajfacebook-github-bot
authored andcommitted
Avoid throwing exceptions when the host activity is not FragmentActivity (#27425)
Summary: There are some code in native modules which currently throws exception when the host activity is not a FragmentActivity, even if the native module is not used. This change avoids the throw and fail the promise instead. ## Changelog There are some code in native modules which currently throws exception when the host activity is not a FragmentActivity, even if the native module is not used. This change avoids the throw and fail the promise instead. [CATEGORY] [TYPE] - Message Pull Request resolved: #27425 Test Plan: We've tested the change on MS Office applications, which currently don't use FragmentActivity. Differential Revision: D18873012 Pulled By: mdvacca fbshipit-source-id: 1b7c9efba5a59b2051487510da9ef7e1232877a5
1 parent 3aba90b commit 7cfabf4

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

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

10+
import android.app.Activity;
1011
import android.app.DatePickerDialog.OnDateSetListener;
1112
import android.content.DialogInterface;
1213
import android.content.DialogInterface.OnDismissListener;
@@ -101,13 +102,16 @@ public void onDismiss(DialogInterface dialog) {
101102
*/
102103
@ReactMethod
103104
public void open(@Nullable final ReadableMap options, Promise promise) {
104-
FragmentActivity activity = (FragmentActivity) getCurrentActivity();
105-
if (activity == null) {
105+
Activity raw_activity = getCurrentActivity();
106+
if (raw_activity == null || !(raw_activity instanceof FragmentActivity)) {
106107
promise.reject(
107-
ERROR_NO_ACTIVITY, "Tried to open a DatePicker dialog while not attached to an Activity");
108+
ERROR_NO_ACTIVITY,
109+
"Tried to open a DatePicker dialog while not attached to a FragmentActivity");
108110
return;
109111
}
110112

113+
FragmentActivity activity = (FragmentActivity) raw_activity;
114+
111115
FragmentManager fragmentManager = activity.getSupportFragmentManager();
112116
DialogFragment oldFragment = (DialogFragment) fragmentManager.findFragmentByTag(FRAGMENT_TAG);
113117
if (oldFragment != null) {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ public void run() {
234234
*/
235235
private @Nullable FragmentManagerHelper getFragmentManagerHelper() {
236236
Activity activity = getCurrentActivity();
237-
if (activity == null) {
237+
if (activity == null || !(activity instanceof FragmentActivity)) {
238238
return null;
239239
}
240240
return new FragmentManagerHelper(((FragmentActivity) activity).getSupportFragmentManager());

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

+8-3
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;
@@ -89,12 +90,16 @@ public void onDismiss(DialogInterface dialog) {
8990
@ReactMethod
9091
public void open(@Nullable final ReadableMap options, Promise promise) {
9192

92-
FragmentActivity activity = (FragmentActivity) getCurrentActivity();
93-
if (activity == null) {
93+
Activity raw_activity = getCurrentActivity();
94+
if (raw_activity == null || !(raw_activity instanceof FragmentActivity)) {
9495
promise.reject(
95-
ERROR_NO_ACTIVITY, "Tried to open a TimePicker dialog while not attached to an Activity");
96+
ERROR_NO_ACTIVITY,
97+
"Tried to open a DatePicker dialog while not attached to a FragmentActivity");
9698
return;
9799
}
100+
101+
FragmentActivity activity = (FragmentActivity) raw_activity;
102+
98103
// We want to support both android.app.Activity and the pre-Honeycomb FragmentActivity
99104
// (for apps that use it for legacy reasons). This unfortunately leads to some code duplication.
100105
FragmentManager fragmentManager = activity.getSupportFragmentManager();

0 commit comments

Comments
 (0)