react native AppState 使用详解

来源:互联网 发布:ktv手机点歌软件 编辑:程序博客网 时间:2024/06/05 22:47

AppState:当前app的状态,是在前台还是在后台。

  • active:应用正在前台运行
  • background:正在后台运行
  • inactive:过渡状态,不会在正常的React Native应用中出现

属性:

addEventListener:添加监听,
removeEventListener:删除监听

demo:

/** * Created by on 2017/5/31. */import React, {Component} from 'react';import {    StyleSheet,    View,    Text,    AppState,} from 'react-native';export default class AppStateDemo extends Component {    static navigationOptions = {        title: 'AppState'    }    state = {        currentAppState:AppState.currentState,    }    componentDidMount() {        AppState.addEventListener('change', this._handleAppStateChange);    }    componentWillUnmount() {        AppState.removeEventListener('change', this._handleAppStateChange);    }    _handleAppStateChange = (nextAppState) => {        this.setState({currentAppState: nextAppState});    }    render() {        return (            <View style={{flex:1}}>                <Text>当前 state 是: {this.state.currentAppState}</Text>            </View>        );    }}

这里写图片描述

github下载地址

原创粉丝点击