2015-09-14 17:35:58 +03:00
|
|
|
/**
|
2019-03-26 20:54:53 +03:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
2015-09-14 17:35:58 +03:00
|
|
|
*
|
2018-02-17 05:24:55 +03:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-09-14 17:35:58 +03:00
|
|
|
*
|
2018-05-11 05:06:46 +03:00
|
|
|
* @format
|
2015-09-14 17:35:58 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2018-05-11 01:44:52 +03:00
|
|
|
const DeviceEventManager = require('NativeModules').DeviceEventManager;
|
|
|
|
const RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');
|
2015-09-14 17:35:58 +03:00
|
|
|
|
2018-05-11 01:44:52 +03:00
|
|
|
const DEVICE_BACK_EVENT = 'hardwareBackPress';
|
2015-09-14 17:35:58 +03:00
|
|
|
|
2019-03-26 20:54:53 +03:00
|
|
|
type BackPressEventName = $Enum<{
|
|
|
|
backPress: string,
|
|
|
|
}>;
|
2015-09-14 17:35:58 +03:00
|
|
|
|
2019-03-26 20:54:53 +03:00
|
|
|
const _backPressSubscriptions = new Set();
|
2015-09-14 17:35:58 +03:00
|
|
|
|
|
|
|
RCTDeviceEventEmitter.addListener(DEVICE_BACK_EVENT, function() {
|
2019-03-26 20:54:53 +03:00
|
|
|
let invokeDefault = true;
|
|
|
|
const subscriptions = Array.from(_backPressSubscriptions.values()).reverse();
|
|
|
|
|
|
|
|
for (let i = 0; i < subscriptions.length; ++i) {
|
|
|
|
if (subscriptions[i]()) {
|
|
|
|
invokeDefault = false;
|
|
|
|
break;
|
2016-08-09 16:32:41 +03:00
|
|
|
}
|
2016-07-21 18:52:07 +03:00
|
|
|
}
|
|
|
|
|
2019-03-26 20:54:53 +03:00
|
|
|
if (invokeDefault) {
|
|
|
|
BackHandler.exitApp();
|
|
|
|
}
|
2015-09-14 17:35:58 +03:00
|
|
|
});
|
|
|
|
|
2015-09-17 17:20:44 +03:00
|
|
|
/**
|
2017-03-07 08:41:51 +03:00
|
|
|
* Detect hardware button presses for back navigation.
|
|
|
|
*
|
|
|
|
* Android: Detect hardware back button presses, and programmatically invoke the default back button
|
2015-09-17 17:20:44 +03:00
|
|
|
* functionality to exit the app if there are no listeners or if none of the listeners return true.
|
2017-03-07 08:41:51 +03:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2019-03-01 21:09:07 +03:00
|
|
|
* macOS: Not applicable.
|
|
|
|
*
|
2016-11-11 11:23:21 +03:00
|
|
|
* 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.
|
2015-09-17 17:20:44 +03:00
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
2016-08-03 04:16:02 +03:00
|
|
|
* ```javascript
|
2017-03-07 08:41:51 +03:00
|
|
|
* BackHandler.addEventListener('hardwareBackPress', function() {
|
2016-08-03 04:16:02 +03:00
|
|
|
* // 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;
|
2015-09-17 17:20:44 +03:00
|
|
|
* });
|
|
|
|
* ```
|
|
|
|
*/
|
2019-03-26 20:54:53 +03:00
|
|
|
const BackHandler = {
|
|
|
|
exitApp: function() {
|
2015-09-14 17:35:58 +03:00
|
|
|
DeviceEventManager.invokeDefaultBackPressHandler();
|
|
|
|
},
|
|
|
|
|
2017-11-07 21:49:26 +03:00
|
|
|
/**
|
|
|
|
* Adds an event handler. Supported events:
|
|
|
|
*
|
|
|
|
* - `hardwareBackPress`: Fires when the Android hardware back button is pressed or when the
|
|
|
|
* tvOS menu button is pressed.
|
|
|
|
*/
|
2018-05-11 05:06:46 +03:00
|
|
|
addEventListener: function(
|
2015-09-14 17:35:58 +03:00
|
|
|
eventName: BackPressEventName,
|
2018-05-11 05:06:46 +03:00
|
|
|
handler: Function,
|
2016-03-29 13:41:23 +03:00
|
|
|
): {remove: () => void} {
|
2019-03-26 20:54:53 +03:00
|
|
|
_backPressSubscriptions.add(handler);
|
2016-03-29 13:41:23 +03:00
|
|
|
return {
|
2019-03-26 20:54:53 +03:00
|
|
|
remove: () => BackHandler.removeEventListener(eventName, handler),
|
2016-03-29 13:41:23 +03:00
|
|
|
};
|
2015-09-14 17:35:58 +03:00
|
|
|
},
|
|
|
|
|
2017-11-07 21:49:26 +03:00
|
|
|
/**
|
|
|
|
* Removes the event handler.
|
|
|
|
*/
|
2015-09-14 17:35:58 +03:00
|
|
|
removeEventListener: function(
|
|
|
|
eventName: BackPressEventName,
|
2018-05-11 05:06:46 +03:00
|
|
|
handler: Function,
|
2015-09-14 17:35:58 +03:00
|
|
|
): void {
|
2019-03-26 20:54:53 +03:00
|
|
|
_backPressSubscriptions.delete(handler);
|
2015-09-14 17:35:58 +03:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2017-03-07 08:41:51 +03:00
|
|
|
module.exports = BackHandler;
|