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