From b8576312ca4a950d8bf4a5f4ec3a80c4f55d88b0 Mon Sep 17 00:00:00 2001 From: Serhij Korochanskyj Date: Thu, 21 Jul 2016 08:52:07 -0700 Subject: [PATCH] Fix BackAndroid subscriptions calls Summary: `BackAndroid` JS event subscriptions should be called in reverse order (the subscription from the latest `addEventLister` should run first). Also if listener returns true, don't call other listeners. **Motivation**: We use `BackAndroid` listeners to prevent closing screens with user's input. When we have two screens in stack (each screen with listener, which show alerts), we want to show alert only from the last screen listener, not from all. Closes https://github.com/facebook/react-native/pull/8929 Differential Revision: D3598978 Pulled By: dmmiller fbshipit-source-id: a7b0762b36a60755a844e90fffd58887f89c9ffb --- Libraries/Utilities/BackAndroid.android.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Libraries/Utilities/BackAndroid.android.js b/Libraries/Utilities/BackAndroid.android.js index 795380448..b459e0b5c 100644 --- a/Libraries/Utilities/BackAndroid.android.js +++ b/Libraries/Utilities/BackAndroid.android.js @@ -25,11 +25,14 @@ var _backPressSubscriptions = new Set(); RCTDeviceEventEmitter.addListener(DEVICE_BACK_EVENT, function() { var backPressSubscriptions = new Set(_backPressSubscriptions); var invokeDefault = true; - backPressSubscriptions.forEach((subscription) => { - if (subscription()) { + var subscriptions = [...backPressSubscriptions].reverse(); + for (var i = 0; i < subscriptions.length; ++i) { + if (subscriptions[i]()) { invokeDefault = false; - } - }); + break; + }; + } + if (invokeDefault) { BackAndroid.exitApp(); }