Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 20 additions & 22 deletions cndocs/appstate.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ id: appstate
title: AppState
---

`AppState` 能告诉你应用当前是在前台还是在后台,并且能在状态变化的时候通知你。
`AppState`能告诉你应用当前是在前台还是在后台,并且能在状态变化的时候通知你。

AppState 通常在处理推送通知的时候用来决定内容和对应的行为。

Expand All @@ -13,19 +13,18 @@ AppState 通常在处理推送通知的时候用来决定内容和对应的行
- `background` - 应用正在后台运行。用户可能面对以下几种情况:
- 在别的应用中
- 停留在桌面
- [Android] 处在另一个 `Activity` 中(即便是由你的应用拉起的)
- [iOS] `inactive` - 此状态表示应用正在前后台的切换过程中,或是处在系统的多任务视图、通知中心,又或是处在来电状态中。
- Android 来说还可能处在另一个`Activity`中(即便是由你的应用拉起的)
- [iOS] `inactive` - 此状态表示应用正在前后台的切换过程中,或是处在系统的多任务视图,又或是处在来电状态中。

要了解更多信息,可以阅读 [Apple 的文档](https://developer.apple.com/documentation/uikit/app_and_scenes/managing_your_app_s_life_cycle)。
要了解更多信息,可以阅读[Apple 的文档](https://developer.apple.com/documentation/uikit/app_and_scenes/managing_your_app_s_life_cycle)。

## 基本用法

要获取当前的状态,你可以使用 `AppState.currentState`,这个变量会一直保持更新。不过在启动的过程中,`currentState` 可能为 null,直到 `AppState` 通过 bridge 获取到值为止
要获取当前的状态,你可以使用`AppState.currentState`,这个变量会一直保持更新。不过在启动的过程中,`currentState`可能为 null,直到`AppState`从原生代码得到通知为止

```SnackPlayer name=AppState%20Example
import React, {useRef, useState, useEffect} from 'react';
import {AppState, StyleSheet, Text} from 'react-native';
import {SafeAreaView, SafeAreaProvider} from 'react-native-safe-area-context';
import {AppState, StyleSheet, Text, View} from 'react-native';

const AppStateExample = () => {
const appState = useRef(AppState.currentState);
Expand All @@ -51,11 +50,9 @@ const AppStateExample = () => {
}, []);

return (
<SafeAreaProvider>
<SafeAreaView style={styles.container}>
<Text>Current state is: {appStateVisible}</Text>
</SafeAreaView>
</SafeAreaProvider>
<View style={styles.container}>
<Text>Current state is: {appStateVisible}</Text>
</View>
);
};

Expand All @@ -70,29 +67,29 @@ const styles = StyleSheet.create({
export default AppStateExample;
```

上面的这个例子只会显示"Current state is: active",这是因为应用只有在 `active` 状态下才能被用户看到。null 状态只会在启动时短暂出现。如果你想尝试这段代码,我们建议使用自己的设备而不是内嵌预览
上面的这个例子只会显示"Current state is: active",这是因为应用只有在`active`状态下才能被用户看到。并且 null 状态只会在一开始的一瞬间出现。如果你想尝试这段代码,我们建议使用自己的设备而不是在上面网页中的嵌入式预览

---

# 文档

## 事件

### `change`
### `blur` <div class="label android">Android</div>

当应用状态发生变化时触发此事件。监听器会收到[当前应用状态值](appstate#app-states)之一作为参数
当用户没有主动与应用程序进行交互时收到。在用户下拉[通知抽屉](https://developer.android.com/guide/topics/ui/notifiers/notifications#bar-and-drawer)的情况下非常有用。`AppState`不会改变,但是`blur`事件将被触发

### `memoryWarning` <div className="label ios">iOS</div>
### `change`

当应用收到操作系统的内存警告时触发
当应用程序状态发生变化时,将接收到此事件。监听器会使用[当前应用程序状态值之一](appstate.md#app-states)来调用
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Fix the link fragment format.

The link uses appstate.md#app-states but should use #app-states for internal navigation within the same document. The filename prefix is unnecessary and may cause navigation issues.

🔗 Proposed fix
-监听器会使用[当前应用程序状态值之一](appstate.md#app-states)来调用。
+监听器会使用[当前应用程序状态值之一](`#app-states`)来调用。
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
当应用程序状态发生变化时,将接收到此事件。监听器会使用[当前应用程序状态值之一](appstate.md#app-states)来调用。
当应用程序状态发生变化时,将接收到此事件。监听器会使用[当前应用程序状态值之一](`#app-states`)来调用。
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@cndocs/appstate.md` at line 84, The link fragment incorrectly includes the
filename prefix "appstate.md#app-states"; update the link to use the internal
fragment only (replace "appstate.md#app-states" with "#app-states") so the
listener documentation references the in-document anchor correctly.


### `focus` <div className="label android">Android</div>
### `focus` <div class="label android">Android</div>

当应用获得焦点时触发(用户正在与应用交互)。
当应用程序获得焦点时收到(用户正在与应用程序进行交互)。

### `blur` <div className="label android">Android</div>
### `memoryWarning`

当用户不再与应用主动交互时触发。在用户下拉[通知抽屉](https://developer.android.com/guide/topics/ui/notifiers/notifications#bar-and-drawer)时非常有用。此时 `AppState` 不会变化,但 `blur` 事件会被触发
这个事件用于在需要时触发内存警告或释放内存

## 方法

Expand All @@ -105,7 +102,8 @@ static addEventListener(
): NativeEventSubscription;
```

设置一个在 AppState 上发生指定事件类型时调用的函数。`eventType` 的有效值参见[上方列出的事件](#events)。返回 `EventSubscription`。
设置一个函数,每当AppState上发生指定的事件类型时将被调用。`eventType` 的有效值为[上面列出的事件](#events)。返回 `EventSubscription`。


## 属性

Expand Down
2 changes: 1 addition & 1 deletion scripts/translate-progress.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
"cndocs/pushnotificationios.md",
"cndocs/text-style-props.md"
],
"updatedAt": "2026-02-22T22:33:21Z"
"updatedAt": "2026-03-06T22:30:00Z"
}