android - 如何在 React Native Es6 上检测 OnResume 和 onPause

标签 android react-native onresume onpause

如何检测从后台到前台 es6 的任何方法?

这在 React-native 中可能吗?是否有任何类或工具可以帮助这种方法?

最佳答案

试试这个,它对我有用

import React, {Component} from 'react'
import {AppState, Text} from 'react-native'

class AppStateExample extends Component {

  state = {
    appState: AppState.currentState
  }

  componentDidMount() {
    AppState.addEventListener('change', this._handleAppStateChange);
  }

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

  _handleAppStateChange = (nextAppState) => {
    if (this.state.appState.match(/inactive|background/) && nextAppState === 'active') {
      console.log('App has come to the foreground!')
    }
    this.setState({appState: nextAppState});
  }

  render() {
    return (
      <Text>Current state is: {this.state.appState}</Text>
    );
  }

}

关于android - 如何在 React Native Es6 上检测 OnResume 和 onPause,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50020342/

相关文章:

javascript - 如果我可以直接访问和使用 this.props 和 this.state 那么为什么我必须在 React Native 中通过构造函数中的 super(props) 调用它们?

android - onResume 在应用程序处于后台或手机 sleep 后未调用

android - onCreate、onCreateOptionsMenu、onResume,执行顺序是什么?

java - 使用 LibGDX SpriteBatches 缩放图像

java - 如何跳过初始数据并仅触发 Firestore Firebase 中的新更新?

java - 在 Android 中 inflatedView 后,自定义 View 子项为空

android - 以编程方式向用户请求权限?

javascript - React 中未调用自定义钩子(Hook)函数

javascript - react native 运行ios类型错误: invalid data

android - 如何从 MainActivity 暂停/恢复 SurfaceView?