Skip to content

Instantly share code, notes, and snippets.

@mkonicek
Created January 12, 2016 14:14
Show Gist options
  • Select an option

  • Save mkonicek/00c3638a4cf5d9ed8db7 to your computer and use it in GitHub Desktop.

Select an option

Save mkonicek/00c3638a4cf5d9ed8db7 to your computer and use it in GitHub Desktop.
/**
* Copyright 2004-present Facebook. All Rights Reserved.
*
* @providesModule AppStateAndroid
*/
'use strict';
var Map = require('Map');
var RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');
var AppState = require('NativeModules').AppStateAndroid;
var logError = require('logError');
var APP_LIFECYCLE_EVENT = 'appLifecycleEvent';
var _appStateHandlers = new Map();
/**
* This class can be used to register to host lifecycle events (ie. paused, resumed).
* Alternatively, one can query AppStateAndroid.currentState to get the current host state.
*/
class AppStateAndroid {
static addEventListener(handler) {
var listener = RCTDeviceEventEmitter.addListener(
APP_LIFECYCLE_EVENT,
(hostLifecycleEventData) => {
handler(hostLifecycleEventData);
}
);
_appStateHandlers.set(handler, listener);
}
static removeEventListener(handler) {
var listener = _appStateHandlers.get(handler);
if (!listener) {
return;
}
listener.remove();
_appStateHandlers.delete(handler);
}
}
AppStateAndroid.currentState = null;
RCTDeviceEventEmitter.addListener(
APP_LIFECYCLE_EVENT,
(hostLifecycleEventData) => {
AppStateAndroid.currentState = hostLifecycleEventData;
}
);
AppState.getCurrentAppState(
(hostLifecycleEventData) => {
AppStateAndroid.currentState = hostLifecycleEventData;
},
logError
);
module.exports = AppStateAndroid;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment