AppState使用

AppState使用

import React, { Component } from 'react';
import {AppRegistry,StyleSheet,Text,View,AppState} from 'react-native';


export default class AppStateApiView extends Component {

  componentWillUnmount() {
      AppState.removeEventListener('change', this.handleAppStateChange);
   }

 componentWillMount() {
       AppState.addEventListener('change', this.handleAppStateChange);
       AppState.addEventListener('memoryWarning', function(){
         console.log("内存报警....");
       });
    }

    //状态改变响应
  handleAppStateChange(appState) {
      alert('当前状态为:'+appState);
      //active前台运行中  background后台运行中 inactive运行的过渡状态
    }

  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.styleText}>
          状态监听中:
        </Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container:{
    flex: 1,
    marginTop:25
  },
  styleText:{
    marginTop:10,
    textAlign:'center'
  },
  styleAppState:{
    marginTop:10,
    color:'red',
    textAlign:'center'
  },
});

 

posted @ 2018-12-28 10:00  新年新气象  阅读(166)  评论(0编辑  收藏  举报