React Native 教程
- 第1节:React 入门
- 第2节:React 安装 Android 运行环境
- 第3节:React Native 样式
- 第4节:React Native 手势应答系统
- 第5节:React Native 辅助功能
- 第6节:React Native 模块(iOS)
- 第7节:React Native UI 组件(iOS)
- 第8节:React Native UI 组件(Android)
- 第9节:React Native 调试 React Native 应用
- 第10节:React Native 测试
- 第11节:React Native 在设备上运行
- 第12节:React Native 在设备上运行(Android)
- 第13节:React Native 与现有的应用程序集成
- 第14节:React Native JavaScript 环境
- 第15节:React Native 已知 Issues
- 第16节:React Native iOS 活动指示器
- 第17节:React Native iOS 日期选择器
- 第18节:React Native DrawerLayoutAndroid
- 第19节:React Native 图片
- 第20节:React Native 列表视图
- 第21节:React Native Map 视图
- 第22节:React Native 导航器
- 第23节:React Native iOS 导航器
- 第24节:React Native iOS 选择器
- 第25节:React Native ProgressBarAndroid
- 第26节:React Native 滚动视图
- 第27节:React Native iOS 滑块
- 第28节:React Native SwitchAndroid
- 第29节:React Native iOS 开关
- 第30节:React Native iOS 选项卡
- 第31节:React Native TabBarIOS.Item
- 第32节:React Native 文本
- 第33节:React Native 文本输入
- 第34节:React Native ToolbarAndroid
- 第35节:React Native 高亮触摸
- 第36节:React Native 不透明触摸
- 第37节:React Native 无反馈触摸
- 第38节:React Native 视图
- 第39节:React Native Web 视图
- 第40节:React Native iOS 警告
- 第41节:React Native 应用程序注册表
- 第42节:React Native iOS 应用程序状态
- 第43节:React Native 异步存储
- 第44节:React Native BackAndroid
- 第45节:React Native 相机滚动
- 第46节:React Native 交互管理器
- 第47节:React Native 动画布局
- 第48节:React Native iOS 链接
- 第49节:React Native 网络信息
- 第50节:React Native 全景响应器
- 第51节:React Native 像素比率
- 第52节:React Native iOS 推送通知
- 第53节:React Native iOS 状态栏
- 第54节:React Native 样式表
- 第55节:React Native ToastAndroid
- 第56节:React Native iOS 震动
- 第57节:React Native Flexbox
- 第58节:React Native 定位
- 第59节:React Native 网络
- 第60节:React Native 计时器
React Native iOS 应用程序状态
AppStateIOS
可以告诉你应用程序是在前台还是在后台,而且状态更新时会通知你。 在处理推送通知时,AppStateIOS 经常被用于判断目标和适当的行为。
iOS 应用程序状态Active - 应用程序在前台运行
Background - 应用程序在后台运行。用户正在使用另一个应用程序或者在主屏幕上。
Inactive - 这是一种过渡状态,目前不会在React Native的应用程序上发生。
Active - 应用程序在前台运行
Background - 应用程序在后台运行。用户正在使用另一个应用程序或者在主屏幕上。
Inactive - 这是一种过渡状态,目前不会在React Native的应用程序上发生。
想要获取更多的信息,见 Apple's documentation
基本用法
为了查看当前的状态,你可以检查 AppStateIOS.currentState
,该方法会一直保持最新状态。然而,当 AppStateIOS
在桥接器上检索currentState
时,在启动时它将会为空。
getInitialState: function() {
return {
currentAppState: AppStateIOS.currentState,
};
}, componentDidMount: function() {
AppStateIOS.addEventListener('change', this._handleAppStateChange);
}, componentWillUnmount: function() {
AppStateIOS.removeEventListener('change', this._handleAppStateChange);
}, _handleAppStateChange: function(currentAppState) {
this.setState({ currentAppState, });
}, render: function() {
return (
<Text>Current state is: {this.state.currentAppState}</Text>
);
},
这个例子似乎只能说"当前状态是:活跃的"因为在 active
状态时,应用程序只对用户是可见的,空状态只能是暂时的。
方法
static addEventListener(type: string, handler: Function)
通过监听 change
事件类型和提供处理程序,为应用程序状态变化添加一个处理程序。
static removeEventListener(type: string, handler: Function)
通过传递 change
事件类型和处理程序,删除一个处理程序。
例子
Edit on GitHub
'use strict'; var React = require('react-native'); var {
AppStateIOS,
Text,
View
} = React; var AppStateSubscription = React.createClass({
getInitialState() { return {
appState: AppStateIOS.currentState,
previousAppStates: [],
};
},
componentDidMount: function() {
AppStateIOS.addEventListener('change', this._handleAppStateChange);
},
componentWillUnmount: function() {
AppStateIOS.removeEventListener('change', this._handleAppStateChange);
},
_handleAppStateChange: function(appState) { var previousAppStates = this.state.previousAppStates.slice();
previousAppStates.push(this.state.appState); this.setState({
appState,
previousAppStates,
});
},
render() { if (this.props.showCurrentOnly) { return ( <View>
<Text>{this.state.appState}</Text>
</View>
);
} return ( <View>
<Text>{JSON.stringify(this.state.previousAppStates)}</Text>
</View>
);
}
});
exports.title = 'AppStateIOS';
exports.description = 'iOS app background status';
exports.examples = [
{
title: 'AppStateIOS.currentState',
description: 'Can be null on app initialization',
render() { return <Text>{AppStateIOS.currentState}</Text>; }
},
{
title: 'Subscribed AppStateIOS:',
description: 'This changes according to the current state, so you can only ever see it rendered as "active"',
render(): ReactElement { return <AppStateSubscription showCurrentOnly={true} />; }
},
{
title: 'Previous states:',
render(): ReactElement { return <AppStateSubscription showCurrentOnly={false} />; }
},
];