Permalink
Browse files

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 #8929

Differential Revision: D3598978

Pulled By: dmmiller

fbshipit-source-id: a7b0762b36a60755a844e90fffd58887f89c9ffb
  • Loading branch information...
1 parent 631785f commit b8576312ca4a950d8bf4a5f4ec3a80c4f55d88b0 @webzepter webzepter committed with Facebook Github Bot 4 Jul 21, 2016
Showing with 7 additions and 4 deletions.
  1. +7 −4 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();
}

0 comments on commit b857631

Please sign in to comment.