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: https://github.com/facebook/react-native/pull/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
This commit is contained in:
Родитель
3aba90b58a
Коммит
7cfabf42b8
|
@ -7,6 +7,7 @@
|
|||
|
||||
package com.facebook.react.modules.datepicker;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.DatePickerDialog.OnDateSetListener;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.DialogInterface.OnDismissListener;
|
||||
|
@ -101,13 +102,16 @@ public class DatePickerDialogModule extends ReactContextBaseJavaModule {
|
|||
*/
|
||||
@ReactMethod
|
||||
public void open(@Nullable final ReadableMap options, Promise promise) {
|
||||
FragmentActivity activity = (FragmentActivity) getCurrentActivity();
|
||||
if (activity == null) {
|
||||
Activity raw_activity = getCurrentActivity();
|
||||
if (raw_activity == null || !(raw_activity instanceof FragmentActivity)) {
|
||||
promise.reject(
|
||||
ERROR_NO_ACTIVITY, "Tried to open a DatePicker dialog while not attached to an Activity");
|
||||
ERROR_NO_ACTIVITY,
|
||||
"Tried to open a DatePicker dialog while not attached to a FragmentActivity");
|
||||
return;
|
||||
}
|
||||
|
||||
FragmentActivity activity = (FragmentActivity) raw_activity;
|
||||
|
||||
FragmentManager fragmentManager = activity.getSupportFragmentManager();
|
||||
DialogFragment oldFragment = (DialogFragment) fragmentManager.findFragmentByTag(FRAGMENT_TAG);
|
||||
if (oldFragment != null) {
|
||||
|
|
|
@ -234,7 +234,7 @@ public class DialogModule extends ReactContextBaseJavaModule implements Lifecycl
|
|||
*/
|
||||
private @Nullable FragmentManagerHelper getFragmentManagerHelper() {
|
||||
Activity activity = getCurrentActivity();
|
||||
if (activity == null) {
|
||||
if (activity == null || !(activity instanceof FragmentActivity)) {
|
||||
return null;
|
||||
}
|
||||
return new FragmentManagerHelper(((FragmentActivity) activity).getSupportFragmentManager());
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
|
||||
package com.facebook.react.modules.timepicker;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.TimePickerDialog.OnTimeSetListener;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.DialogInterface.OnDismissListener;
|
||||
|
@ -89,12 +90,16 @@ public class TimePickerDialogModule extends ReactContextBaseJavaModule {
|
|||
@ReactMethod
|
||||
public void open(@Nullable final ReadableMap options, Promise promise) {
|
||||
|
||||
FragmentActivity activity = (FragmentActivity) getCurrentActivity();
|
||||
if (activity == null) {
|
||||
Activity raw_activity = getCurrentActivity();
|
||||
if (raw_activity == null || !(raw_activity instanceof FragmentActivity)) {
|
||||
promise.reject(
|
||||
ERROR_NO_ACTIVITY, "Tried to open a TimePicker dialog while not attached to an Activity");
|
||||
ERROR_NO_ACTIVITY,
|
||||
"Tried to open a DatePicker dialog while not attached to a FragmentActivity");
|
||||
return;
|
||||
}
|
||||
|
||||
FragmentActivity activity = (FragmentActivity) raw_activity;
|
||||
|
||||
// We want to support both android.app.Activity and the pre-Honeycomb FragmentActivity
|
||||
// (for apps that use it for legacy reasons). This unfortunately leads to some code duplication.
|
||||
FragmentManager fragmentManager = activity.getSupportFragmentManager();
|
||||
|
|
Загрузка…
Ссылка в новой задаче