2015-09-14 14:35:58 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
2017-03-07 05:41:51 +00:00
|
|
|
* @providesModule BackHandler
|
2015-09-14 14:35:58 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var DeviceEventManager = require('NativeModules').DeviceEventManager;
|
|
|
|
var RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');
|
|
|
|
|
|
|
|
var DEVICE_BACK_EVENT = 'hardwareBackPress';
|
|
|
|
|
|
|
|
type BackPressEventName = $Enum<{
|
2016-08-09 13:32:41 +00:00
|
|
|
backPress: string,
|
2015-09-14 14:35:58 +00:00
|
|
|
}>;
|
|
|
|
|
2016-03-29 10:41:23 +00:00
|
|
|
var _backPressSubscriptions = new Set();
|
2015-09-14 14:35:58 +00:00
|
|
|
|
|
|
|
RCTDeviceEventEmitter.addListener(DEVICE_BACK_EVENT, function() {
|
2016-03-29 10:41:23 +00:00
|
|
|
var backPressSubscriptions = new Set(_backPressSubscriptions);
|
|
|
|
var invokeDefault = true;
|
2016-07-21 15:52:07 +00:00
|
|
|
var subscriptions = [...backPressSubscriptions].reverse();
|
|
|
|
for (var i = 0; i < subscriptions.length; ++i) {
|
|
|
|
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
|
|
|
* });
|
|
|
|
* ```
|
|
|
|
*/
|
2017-03-07 05:41:51 +00:00
|
|
|
var BackHandler = {
|
2015-09-14 14:35:58 +00:00
|
|
|
|
|
|
|
exitApp: function() {
|
|
|
|
DeviceEventManager.invokeDefaultBackPressHandler();
|
|
|
|
},
|
|
|
|
|
|
|
|
addEventListener: function (
|
|
|
|
eventName: BackPressEventName,
|
|
|
|
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
|
|
|
},
|
|
|
|
|
|
|
|
removeEventListener: function(
|
|
|
|
eventName: BackPressEventName,
|
|
|
|
handler: Function
|
|
|
|
): 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;
|