Fix that BackHandler was not called on Android (#19077)

Summary:
This fixes issues #18954 and #15497

The transformation from Set to (an reversed) Array does not work on Android devices when the remote debugging was started with `react-native run-android`.

<!--
  Required: Write your motivation here.
  If this PR fixes an issue, type "Fixes #issueNumber" to automatically close the issue when the PR is merged.
-->

Add the following code to your App.js and run the app with `react-native run-android` in an Android device:

```
  componentDidMount() {
    BackHandler.addEventListener('hardwareBackPress', () => {
      console.warn('Callback called. Do not close app. Do nothing at all.');
      return true; // Do not close the app.
    });
  }
```

After adding this, the app should NOT close when you press the back button. Without this fix, the app closes (which is the default behaviour).

See also the comments from Victoriayangx in #18954 and #15497.

<!--
  Does this PR require a documentation change?
  Create a PR at https://github.com/facebook/react-native-website and add a link to it here.
-->

<!--
  Required.
  Help reviewers and the release process by writing your own release notes. See below for an example.
-->

[ANDROID] [BUGFIX] [BackHandler] - Fix that BackHandler was not called on Android

<!--
  **INTERNAL and MINOR tagged notes will not be included in the next version's final release notes.**

    CATEGORY
  [----------]      TYPE
  [ CLI      ] [-------------]    LOCATION
  [ DOCS     ] [ BREAKING    ] [-------------]
  [ GENERAL  ] [ BUGFIX      ] [ {Component} ]
  [ INTERNAL ] [ ENHANCEMENT ] [ {Filename}  ]
  [ IOS      ] [ FEATURE     ] [ {Directory} ]   |-----------|
  [ ANDROID  ] [ MINOR       ] [ {Framework} ] - | {Message} |
  [----------] [-------------] [-------------]   |-----------|

 EXAMPLES:

 [IOS] [BREAKING] [FlatList] - Change a thing that breaks other things
 [ANDROID] [BUGFIX] [TextInput] - Did a thing to TextInput
 [CLI] [FEATURE] [local-cli/info/info.js] - CLI easier to do things with
 [DOCS] [BUGFIX] [GettingStarted.md] - Accidentally a thing/word
 [GENERAL] [ENHANCEMENT] [Yoga] - Added new yoga thing/position
 [INTERNAL] [FEATURE] [./scripts] - Added thing to script that nobody will see
-->
Pull Request resolved: https://github.com/facebook/react-native/pull/19077

Reviewed By: yns88

Differential Revision: D9692298

Pulled By: hramos

fbshipit-source-id: 4526b07a4924055ebc9c42e02615c27aa5c97fbb
This commit is contained in:
Christoph Jerolimov 2018-09-06 20:25:32 -07:00 committed by Facebook Github Bot
parent 0cce0a62c1
commit c3e42b5531

View File

@ -18,22 +18,16 @@ type BackPressEventName = $Enum<{
backPress: string,
}>;
const _backPressSubscriptions = new Set();
const _backPressSubscriptions = [];
RCTDeviceEventEmitter.addListener(DEVICE_BACK_EVENT, function() {
let invokeDefault = true;
const subscriptions = Array.from(_backPressSubscriptions.values()).reverse();
for (let i = 0; i < subscriptions.length; ++i) {
if (subscriptions[i]()) {
invokeDefault = false;
break;
for (let i = _backPressSubscriptions.length - 1; i >= 0; i--) {
if (_backPressSubscriptions[i]()) {
return;
}
}
if (invokeDefault) {
BackHandler.exitApp();
}
BackHandler.exitApp();
});
/**
@ -81,7 +75,9 @@ const BackHandler = {
eventName: BackPressEventName,
handler: Function,
): {remove: () => void} {
_backPressSubscriptions.add(handler);
if (_backPressSubscriptions.indexOf(handler) === -1) {
_backPressSubscriptions.push(handler);
}
return {
remove: () => BackHandler.removeEventListener(eventName, handler),
};
@ -94,7 +90,12 @@ const BackHandler = {
eventName: BackPressEventName,
handler: Function,
): void {
_backPressSubscriptions.delete(handler);
if (_backPressSubscriptions.indexOf(handler) !== -1) {
_backPressSubscriptions.splice(
_backPressSubscriptions.indexOf(handler),
1,
);
}
},
};