2016-01-21 19:43:51 +00:00
|
|
|
/**
|
2017-05-06 03:50:47 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
2016-05-25 11:17:35 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2016-01-21 19:43:51 +00:00
|
|
|
* @providesModule AppStateExample
|
|
|
|
* @flow
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2016-05-25 11:17:35 +00:00
|
|
|
const React = require('react');
|
|
|
|
const ReactNative = require('react-native');
|
|
|
|
const {
|
2016-01-21 19:43:51 +00:00
|
|
|
AppState,
|
|
|
|
Text,
|
|
|
|
View
|
2016-04-09 03:36:40 +00:00
|
|
|
} = ReactNative;
|
2016-01-21 19:43:51 +00:00
|
|
|
|
2017-08-18 01:36:54 +00:00
|
|
|
class AppStateSubscription extends React.Component<$FlowFixMeProps, $FlowFixMeState> {
|
2016-07-26 08:00:02 +00:00
|
|
|
state = {
|
|
|
|
appState: AppState.currentState,
|
|
|
|
previousAppStates: [],
|
|
|
|
memoryWarnings: 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
componentDidMount() {
|
2016-01-21 19:43:51 +00:00
|
|
|
AppState.addEventListener('change', this._handleAppStateChange);
|
2016-05-25 11:17:35 +00:00
|
|
|
AppState.addEventListener('memoryWarning', this._handleMemoryWarning);
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2016-01-21 19:43:51 +00:00
|
|
|
AppState.removeEventListener('change', this._handleAppStateChange);
|
2016-05-25 11:17:35 +00:00
|
|
|
AppState.removeEventListener('memoryWarning', this._handleMemoryWarning);
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_handleMemoryWarning = () => {
|
2016-05-25 11:17:35 +00:00
|
|
|
this.setState({memoryWarnings: this.state.memoryWarnings + 1});
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
_handleAppStateChange = (appState) => {
|
2016-01-21 19:43:51 +00:00
|
|
|
var previousAppStates = this.state.previousAppStates.slice();
|
|
|
|
previousAppStates.push(this.state.appState);
|
|
|
|
this.setState({
|
|
|
|
appState,
|
|
|
|
previousAppStates,
|
|
|
|
});
|
2016-07-26 08:00:02 +00:00
|
|
|
};
|
|
|
|
|
2016-01-21 19:43:51 +00:00
|
|
|
render() {
|
2016-05-25 11:17:35 +00:00
|
|
|
if (this.props.showMemoryWarnings) {
|
|
|
|
return (
|
|
|
|
<View>
|
|
|
|
<Text>{this.state.memoryWarnings}</Text>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
2016-01-21 19:43:51 +00:00
|
|
|
if (this.props.showCurrentOnly) {
|
|
|
|
return (
|
|
|
|
<View>
|
|
|
|
<Text>{this.state.appState}</Text>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<View>
|
|
|
|
<Text>{JSON.stringify(this.state.previousAppStates)}</Text>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2016-01-21 19:43:51 +00:00
|
|
|
|
|
|
|
exports.title = 'AppState';
|
|
|
|
exports.description = 'app background status';
|
|
|
|
exports.examples = [
|
|
|
|
{
|
|
|
|
title: 'AppState.currentState',
|
|
|
|
description: 'Can be null on app initialization',
|
|
|
|
render() { return <Text>{AppState.currentState}</Text>; }
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Subscribed AppState:',
|
|
|
|
description: 'This changes according to the current state, so you can only ever see it rendered as "active"',
|
2016-10-16 11:11:59 +00:00
|
|
|
render(): React.Element<any> { return <AppStateSubscription showCurrentOnly={true} />; }
|
2016-01-21 19:43:51 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Previous states:',
|
2016-10-16 11:11:59 +00:00
|
|
|
render(): React.Element<any> { return <AppStateSubscription showCurrentOnly={false} />; }
|
2016-01-21 19:43:51 +00:00
|
|
|
},
|
2016-05-25 11:17:35 +00:00
|
|
|
{
|
|
|
|
platform: 'ios',
|
|
|
|
title: 'Memory Warnings',
|
|
|
|
description: 'In the IOS simulator, hit Shift+Command+M to simulate a memory warning.',
|
2016-10-16 11:11:59 +00:00
|
|
|
render(): React.Element<any> { return <AppStateSubscription showMemoryWarnings={true} />; }
|
2016-05-25 11:17:35 +00:00
|
|
|
},
|
2016-01-21 19:43:51 +00:00
|
|
|
];
|