@@ -3,7 +3,7 @@ id: appstate
33title : AppState
44---
55
6- ` AppState ` 能告诉你应用当前是在前台还是在后台,并且能在状态变化的时候通知你。
6+ ` AppState ` 能告诉你应用当前是在前台还是在后台,并且能在状态变化的时候通知你。
77
88AppState 通常在处理推送通知的时候用来决定内容和对应的行为。
99
@@ -13,19 +13,18 @@ AppState 通常在处理推送通知的时候用来决定内容和对应的行
1313- ` background ` - 应用正在后台运行。用户可能面对以下几种情况:
1414 - 在别的应用中
1515 - 停留在桌面
16- - [ Android] 处在另一个 ` Activity ` 中(即便是由你的应用拉起的)
17- - [ iOS] ` inactive ` - 此状态表示应用正在前后台的切换过程中,或是处在系统的多任务视图、通知中心 ,又或是处在来电状态中。
16+ - 对 Android 来说还可能处在另一个 ` Activity ` 中(即便是由你的应用拉起的)
17+ - [ iOS] ` inactive ` - 此状态表示应用正在前后台的切换过程中,或是处在系统的多任务视图,又或是处在来电状态中。
1818
19- 要了解更多信息,可以阅读 [ Apple 的文档] ( https://developer.apple.com/documentation/uikit/app_and_scenes/managing_your_app_s_life_cycle ) 。
19+ 要了解更多信息,可以阅读[ Apple 的文档] ( https://developer.apple.com/documentation/uikit/app_and_scenes/managing_your_app_s_life_cycle ) 。
2020
2121## 基本用法
2222
23- 要获取当前的状态,你可以使用 ` AppState.currentState ` ,这个变量会一直保持更新。不过在启动的过程中,` currentState ` 可能为 null,直到 ` AppState ` 通过 bridge 获取到值为止 。
23+ 要获取当前的状态,你可以使用` AppState.currentState ` ,这个变量会一直保持更新。不过在启动的过程中,` currentState ` 可能为 null,直到` AppState ` 从原生代码得到通知为止 。
2424
2525``` SnackPlayer name=AppState%20Example
2626import React, {useRef, useState, useEffect} from 'react';
27- import {AppState, StyleSheet, Text} from 'react-native';
28- import {SafeAreaView, SafeAreaProvider} from 'react-native-safe-area-context';
27+ import {AppState, StyleSheet, Text, View} from 'react-native';
2928
3029const AppStateExample = () => {
3130 const appState = useRef(AppState.currentState);
@@ -51,11 +50,9 @@ const AppStateExample = () => {
5150 }, []);
5251
5352 return (
54- <SafeAreaProvider>
55- <SafeAreaView style={styles.container}>
56- <Text>Current state is: {appStateVisible}</Text>
57- </SafeAreaView>
58- </SafeAreaProvider>
53+ <View style={styles.container}>
54+ <Text>Current state is: {appStateVisible}</Text>
55+ </View>
5956 );
6057};
6158
@@ -70,29 +67,29 @@ const styles = StyleSheet.create({
7067export default AppStateExample;
7168```
7269
73- 上面的这个例子只会显示"Current state is: active",这是因为应用只有在 ` active ` 状态下才能被用户看到。null 状态只会在启动时短暂出现 。如果你想尝试这段代码,我们建议使用自己的设备而不是内嵌预览 。
70+ 上面的这个例子只会显示"Current state is: active",这是因为应用只有在` active ` 状态下才能被用户看到。并且 null 状态只会在一开始的一瞬间出现 。如果你想尝试这段代码,我们建议使用自己的设备而不是在上面网页中的嵌入式预览 。
7471
7572---
7673
7774# 文档
7875
7976## 事件
8077
81- ### ` change `
78+ ### ` blur ` < div class = " label android " >Android</ div >
8279
83- 当应用状态发生变化时触发此事件。监听器会收到 [ 当前应用状态值 ] ( appstate#app-states ) 之一作为参数 。
80+ 当用户没有主动与应用程序进行交互时收到。在用户下拉 [ 通知抽屉 ] ( https://developer.android.com/guide/topics/ui/notifiers/notifications#bar-and-drawer ) 的情况下非常有用。 ` AppState ` 不会改变,但是 ` blur ` 事件将被触发 。
8481
85- ### ` memoryWarning ` < div className = " label ios " >iOS</ div >
82+ ### ` change `
8683
87- 当应用收到操作系统的内存警告时触发 。
84+ 当应用程序状态发生变化时,将接收到此事件。监听器会使用 [ 当前应用程序状态值之一 ] ( appstate.md#app-states ) 来调用 。
8885
89- ### ` focus ` <div className =" label android " >Android</div >
86+ ### ` focus ` <div class =" label android " >Android</div >
9087
91- 当应用获得焦点时触发(用户正在与应用交互 )。
88+ 当应用程序获得焦点时收到(用户正在与应用程序进行交互 )。
9289
93- ### ` blur ` < div className = " label android " >Android</ div >
90+ ### ` memoryWarning `
9491
95- 当用户不再与应用主动交互时触发。在用户下拉 [ 通知抽屉 ] ( https://developer.android.com/guide/topics/ui/notifiers/notifications#bar-and-drawer ) 时非常有用。此时 ` AppState ` 不会变化,但 ` blur ` 事件会被触发 。
92+ 这个事件用于在需要时触发内存警告或释放内存 。
9693
9794## 方法
9895
@@ -105,7 +102,8 @@ static addEventListener(
105102): NativeEventSubscription ;
106103```
107104
108- 设置一个在 AppState 上发生指定事件类型时调用的函数。` eventType ` 的有效值参见[ 上方列出的事件] ( #events ) 。返回 ` EventSubscription ` 。
105+ 设置一个函数,每当AppState上发生指定的事件类型时将被调用。` eventType ` 的有效值为[ 上面列出的事件] ( #events ) 。返回 ` EventSubscription ` 。
106+
109107
110108## 属性
111109
0 commit comments