Fix BadTokenException and IllegalArgmentException thrown when showing or dismissing Modal

Reviewed By: achen1

Differential Revision: D6531148

fbshipit-source-id: 1d3f6e041ac68ad13120c56ea5223557fca34f0d
This commit is contained in:
David Vacca 2017-12-11 09:34:48 -08:00 committed by Facebook Github Bot
parent 1d16923063
commit c465599a27
1 changed files with 15 additions and 5 deletions

View File

@ -128,7 +128,10 @@ public class ReactModalHostView extends ViewGroup implements LifecycleEventListe
private void dismiss() { private void dismiss() {
if (mDialog != null) { if (mDialog != null) {
mDialog.dismiss(); Activity currentActivity = getCurrentActivity();
if (mDialog.isShowing() && (currentActivity == null || !currentActivity.isFinishing())) {
mDialog.dismiss();
}
mDialog = null; mDialog = null;
// We need to remove the mHostView from the parent // We need to remove the mHostView from the parent
@ -209,8 +212,9 @@ public class ReactModalHostView extends ViewGroup implements LifecycleEventListe
} else if (mAnimationType.equals("slide")) { } else if (mAnimationType.equals("slide")) {
theme = R.style.Theme_FullScreenDialogAnimatedSlide; theme = R.style.Theme_FullScreenDialogAnimatedSlide;
} }
mDialog = new Dialog(getContext(), theme); Activity currentActivity = getCurrentActivity();
Context context = currentActivity == null ? getContext() : currentActivity;
mDialog = new Dialog(context, theme);
mDialog.setContentView(getContentView()); mDialog.setContentView(getContentView());
updateProperties(); updateProperties();
@ -233,7 +237,7 @@ public class ReactModalHostView extends ViewGroup implements LifecycleEventListe
} else { } else {
// We redirect the rest of the key events to the current activity, since the activity // We redirect the rest of the key events to the current activity, since the activity
// expects to receive those events and react to them, ie. in the case of the dev menu // expects to receive those events and react to them, ie. in the case of the dev menu
Activity currentActivity = ((ReactContext) getContext()).getCurrentActivity(); Activity currentActivity = getCurrentActivity();
if (currentActivity != null) { if (currentActivity != null) {
return currentActivity.onKeyUp(keyCode, event); return currentActivity.onKeyUp(keyCode, event);
} }
@ -247,7 +251,13 @@ public class ReactModalHostView extends ViewGroup implements LifecycleEventListe
if (mHardwareAccelerated) { if (mHardwareAccelerated) {
mDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED); mDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
} }
mDialog.show(); if (currentActivity == null || !currentActivity.isFinishing()) {
mDialog.show();
}
}
private @Nullable Activity getCurrentActivity() {
return ((ReactContext) getContext()).getCurrentActivity();
} }
/** /**