From adaf04044bc8a05862b8f1be801c5c93d0d7fce5 Mon Sep 17 00:00:00 2001 From: Sergei Dryganets Date: Fri, 29 Dec 2017 12:34:28 -0800 Subject: [PATCH] Bug fix for https://github.com/facebook/react-native/issues/6228. Test Plan: Check work with the dialogs. The original issue occurs in case of calling the showDialog after activity state has been saved. The mIsInForeground flag accessed from two different threads as result module doesnt' work as expected. Release Notes: Fixed race-condition in DialogModule on android platform. We had an access to the mIsInForeground flag on two different threads. --- .../react/modules/dialog/DialogModule.java | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/dialog/DialogModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/dialog/DialogModule.java index d543b6cf083c5c..6c2f39d180d691 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/dialog/DialogModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/dialog/DialogModule.java @@ -28,6 +28,7 @@ import com.facebook.react.bridge.ReactMethod; import com.facebook.react.bridge.ReadableArray; import com.facebook.react.bridge.ReadableMap; +import com.facebook.react.bridge.UiThreadUtil; import com.facebook.react.common.MapBuilder; import com.facebook.react.module.annotations.ReactModule; @@ -95,6 +96,7 @@ public FragmentManagerHelper(android.app.FragmentManager fragmentManager) { } public void showPendingAlert() { + UiThreadUtil.assertOnUiThread(); if (mFragmentToShow == null) { return; } @@ -123,6 +125,8 @@ private void dismissExisting() { } public void showNewAlert(boolean isInForeground, Bundle arguments, Callback actionCallback) { + UiThreadUtil.assertOnUiThread(); + dismissExisting(); AlertFragmentListener actionListener = @@ -218,8 +222,8 @@ public void onHostResume() { public void showAlert( ReadableMap options, Callback errorCallback, - Callback actionCallback) { - FragmentManagerHelper fragmentManagerHelper = getFragmentManagerHelper(); + final Callback actionCallback) { + final FragmentManagerHelper fragmentManagerHelper = getFragmentManagerHelper(); if (fragmentManagerHelper == null) { errorCallback.invoke("Tried to show an alert while not attached to an Activity"); return; @@ -253,7 +257,13 @@ public void showAlert( args.putBoolean(KEY_CANCELABLE, options.getBoolean(KEY_CANCELABLE)); } - fragmentManagerHelper.showNewAlert(mIsInForeground, args, actionCallback); + UiThreadUtil.runOnUiThread(new Runnable() { + @Override + public void run() { + fragmentManagerHelper.showNewAlert(mIsInForeground, args, actionCallback); + } + }); + } /**