diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostView.java b/ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostView.java index 674c6ef80675b6..8dfa9dd40209a5 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostView.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostView.java @@ -128,7 +128,10 @@ public void onDropInstance() { private void dismiss() { if (mDialog != null) { - mDialog.dismiss(); + Activity currentActivity = getCurrentActivity(); + if (mDialog.isShowing() && (currentActivity == null || !currentActivity.isFinishing())) { + mDialog.dismiss(); + } mDialog = null; // We need to remove the mHostView from the parent @@ -168,8 +171,7 @@ public void onHostResume() { @Override public void onHostPause() { - // We dismiss the dialog and reconstitute it onHostResume - dismiss(); + // do nothing } @Override @@ -183,6 +185,10 @@ public void onHostDestroy() { return mDialog; } + private @Nullable Activity getCurrentActivity() { + return ((ReactContext) getContext()).getCurrentActivity(); + } + /** * showOrUpdate will display the Dialog. It is called by the manager once all properties are set * because we need to know all of them before creating the Dialog. It is also smart during @@ -209,7 +215,9 @@ protected void showOrUpdate() { } else if (mAnimationType.equals("slide")) { 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()); updateProperties(); @@ -247,7 +255,9 @@ public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) { if (mHardwareAccelerated) { mDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED); } - mDialog.show(); + if (currentActivity == null || !currentActivity.isFinishing()) { + mDialog.show(); + } } /**