@@ -3,7 +3,7 @@ id: appstate
33title : AppState
44---
55
6- ` AppState ` 能告诉你应用当前是在前台还是在后台,并且能在状态变化的时候通知你。
6+ ` AppState ` 能告诉你应用当前是在前台还是在后台,并且能在状态变化的时候通知你。
77
88AppState 通常在处理推送通知的时候用来决定内容和对应的行为。
99
@@ -13,18 +13,19 @@ 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 ` 从原生代码得到通知为止 。
23+ 要获取当前的状态,你可以使用 ` AppState.currentState ` ,这个变量会一直保持更新。不过在启动的过程中,` currentState ` 可能为 null,直到 ` AppState ` 通过 bridge 获取到值为止 。
2424
2525``` SnackPlayer name=AppState%20Example
2626import React, {useRef, useState, useEffect} from 'react';
27- import {AppState, StyleSheet, Text, View} from 'react-native';
27+ import {AppState, StyleSheet, Text} from 'react-native';
28+ import {SafeAreaView, SafeAreaProvider} from 'react-native-safe-area-context';
2829
2930const AppStateExample = () => {
3031 const appState = useRef(AppState.currentState);
@@ -50,9 +51,11 @@ const AppStateExample = () => {
5051 }, []);
5152
5253 return (
53- <View style={styles.container}>
54- <Text>Current state is: {appStateVisible}</Text>
55- </View>
54+ <SafeAreaProvider>
55+ <SafeAreaView style={styles.container}>
56+ <Text>Current state is: {appStateVisible}</Text>
57+ </SafeAreaView>
58+ </SafeAreaProvider>
5659 );
5760};
5861
@@ -67,29 +70,29 @@ const styles = StyleSheet.create({
6770export default AppStateExample;
6871```
6972
70- 上面的这个例子只会显示"Current state is: active",这是因为应用只有在` active ` 状态下才能被用户看到。并且 null 状态只会在一开始的一瞬间出现 。如果你想尝试这段代码,我们建议使用自己的设备而不是在上面网页中的嵌入式预览 。
73+ 上面的这个例子只会显示"Current state is: active",这是因为应用只有在 ` active ` 状态下才能被用户看到。null 状态只会在启动时短暂出现 。如果你想尝试这段代码,我们建议使用自己的设备而不是内嵌预览 。
7174
7275---
7376
7477# 文档
7578
7679## 事件
7780
78- ### ` blur ` < div class = " label android " >Android</ div >
81+ ### ` change `
7982
80- 当用户没有主动与应用程序进行交互时收到。在用户下拉 [ 通知抽屉 ] ( https://developer.android.com/guide/topics/ui/notifiers/notifications#bar-and-drawer ) 的情况下非常有用。 ` AppState ` 不会改变,但是 ` blur ` 事件将被触发 。
83+ 当应用状态发生变化时触发此事件。监听器会收到 [ 当前应用状态值 ] ( appstate#app-states ) 之一作为参数 。
8184
82- ### ` change `
85+ ### ` memoryWarning ` < div className = " label ios " >iOS</ div >
8386
84- 当应用程序状态发生变化时,将接收到此事件。监听器会使用 [ 当前应用程序状态值之一 ] ( appstate.md#app-states ) 来调用 。
87+ 当应用收到操作系统的内存警告时触发 。
8588
86- ### ` focus ` <div class =" label android " >Android</div >
89+ ### ` focus ` <div className =" label android " >Android</div >
8790
88- 当应用程序获得焦点时收到(用户正在与应用程序进行交互 )。
91+ 当应用获得焦点时触发(用户正在与应用交互 )。
8992
90- ### ` memoryWarning `
93+ ### ` blur ` < div className = " label android " >Android</ div >
9194
92- 这个事件用于在需要时触发内存警告或释放内存 。
95+ 当用户不再与应用主动交互时触发。在用户下拉 [ 通知抽屉 ] ( https://developer.android.com/guide/topics/ui/notifiers/notifications#bar-and-drawer ) 时非常有用。此时 ` AppState ` 不会变化,但 ` blur ` 事件会被触发 。
9396
9497## 方法
9598
@@ -102,8 +105,7 @@ static addEventListener(
102105): NativeEventSubscription ;
103106```
104107
105- 设置一个函数,每当AppState上发生指定的事件类型时将被调用。` eventType ` 的有效值为[ 上面列出的事件] ( #events ) 。返回 ` EventSubscription ` 。
106-
108+ 设置一个在 AppState 上发生指定事件类型时调用的函数。` eventType ` 的有效值参见[ 上方列出的事件] ( #events ) 。返回 ` EventSubscription ` 。
107109
108110## 属性
109111
0 commit comments