react-native-macos/Libraries/Utilities/BackHandler.android.js

117 строки
3.2 KiB
JavaScript
Исходник Обычный вид История

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
*/
'use strict';
import NativeDeviceEventManager from '../../Libraries/NativeModules/specs/NativeDeviceEventManager';
import RCTDeviceEventEmitter from '../EventEmitter/RCTDeviceEventEmitter';
const DEVICE_BACK_EVENT = 'hardwareBackPress';
type BackPressEventName = 'backPress' | 'hardwareBackPress';
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
2018-09-07 06:25:32 +03:00
const _backPressSubscriptions = [];
RCTDeviceEventEmitter.addListener(DEVICE_BACK_EVENT, function() {
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
2018-09-07 06:25:32 +03:00
for (let i = _backPressSubscriptions.length - 1; i >= 0; i--) {
if (_backPressSubscriptions[i]()) {
return;
}
}
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
2018-09-07 06:25:32 +03:00
BackHandler.exitApp();
});
/**
* Detect hardware button presses for back navigation.
*
* Android: Detect hardware back button presses, and programmatically invoke the default back button
* functionality to exit the app if there are no listeners or if none of the listeners return true.
*
* tvOS: Detect presses of the menu button on the TV remote. (Still to be implemented:
* programmatically disable menu button handling
* functionality to exit the app if there are no listeners or if none of the listeners return true.)
*
* iOS: Not applicable.
*
* The event subscriptions are called in reverse order (i.e. last registered subscription first),
* and if one subscription returns true then subscriptions registered earlier will not be called.
*
* Example:
*
* ```javascript
* BackHandler.addEventListener('hardwareBackPress', function() {
* // this.onMainScreen and this.goBack are just examples, you need to use your own implementation here
* // Typically you would use the navigator here to go to the last state.
*
* if (!this.onMainScreen()) {
* this.goBack();
* return true;
* }
* return false;
* });
* ```
*/
type TBackHandler = {|
+exitApp: () => void,
+addEventListener: (
eventName: BackPressEventName,
handler: Function,
) => {remove: () => void, ...},
+removeEventListener: (
eventName: BackPressEventName,
handler: Function,
) => void,
|};
const BackHandler: TBackHandler = {
exitApp: function(): void {
if (!NativeDeviceEventManager) {
return;
}
NativeDeviceEventManager.invokeDefaultBackPressHandler();
},
/**
* Adds an event handler. Supported events:
*
* - `hardwareBackPress`: Fires when the Android hardware back button is pressed or when the
* tvOS menu button is pressed.
*/
addEventListener: function(
eventName: BackPressEventName,
handler: Function,
): {remove: () => void, ...} {
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
2018-09-07 06:25:32 +03:00
if (_backPressSubscriptions.indexOf(handler) === -1) {
_backPressSubscriptions.push(handler);
}
return {
remove: (): void => BackHandler.removeEventListener(eventName, handler),
};
},
/**
* Removes the event handler.
*/
removeEventListener: function(
eventName: BackPressEventName,
handler: Function,
): void {
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
2018-09-07 06:25:32 +03:00
if (_backPressSubscriptions.indexOf(handler) !== -1) {
_backPressSubscriptions.splice(
_backPressSubscriptions.indexOf(handler),
1,
);
}
},
};
module.exports = BackHandler;