在 Android 上恢复状态
如何在操作系统终止应用后恢复 Android 应用的状态。
当用户运行移动应用后选择运行另一个应用时,第一个应用会进入后台,或称 被置于后台。操作系统(iOS 与 Android 均如此)可能会终止后台应用以释放内存,并提升前台应用的性能。
当用户再次选择该应用、将其带回前台时,操作系统会重新启动它。但除非你在应用被终止前设置了保存状态的方式,否则状态会丢失,应用会从头开始。用户会失去他们期望的连续性,这显然不理想。(想象在点击 Submit 之前 填写冗长表单时被电话打断。)
那么,如何恢复应用状态,使其看起来与进入后台之前一样?
Flutter 在 services 库中通过
RestorationManager(及相关类)提供解决方案。使用 RestorationManager 时,Flutter 框架会在 状态变化时 将状态数据提供给引擎,以便在操作系统发出即将终止应用的信号时应用已就绪,而应用只有片刻时间准备。
概述
#只需完成几项任务即可启用状态恢复:
-
Define a
restorationScopeIdfor classes likeCupertinoApp,MaterialApp, orWidgetsApp. -
为
CupertinoApp、MaterialApp或WidgetsApp等类定义restorationScopeId。 -
Define a
restorationIdfor widgets that support it, such asTextFieldandScrollView. This automatically enables built-in state restoration for those widgets. -
为支持它的 widget(如
TextField和ScrollView)定义restorationId。这会自动为这些 widget 启用内置状态恢复。 -
For custom widgets, you must decide what state you want to restore and hold that state in a
RestorableProperty. (The Flutter API provides various subclasses for different data types.) Define thoseRestorablePropertywidgets in aStateclass that uses theRestorationMixin. Register those widgets with the mixin in arestoreStatemethod. -
对于自定义 widget,你必须决定要恢复哪些状态,并在
RestorableProperty中保存该状态。(Flutter API 为不同数据类型提供多种子类。)在使用RestorationMixin的State类中定义这些RestorablePropertywidget。在restoreState方法中向 mixin 注册这些 widget。 -
If you use any Navigator API (like
push,pushNamed, and so on) migrate to the API that has "restorable" in the name (restorablePush,restorablePushNamed, and so on) to restore the navigation stack. -
若使用任何 Navigator API(如
push、pushNamed等),请迁移到名称中包含restorable的 API (restorablePush、restorablePushNamed等)以恢复导航栈。
其他注意事项:
-
Providing a
restorationScopeIdtoMaterialApp,CupertinoApp, orWidgetsAppautomatically enables state restoration by injecting aRootRestorationScope. If you need to restore state above the app class, inject aRootRestorationScopemanually. -
向
MaterialApp、CupertinoApp或WidgetsApp提供restorationScopeId会通过注入RootRestorationScope自动启用状态恢复。若需要在应用类 之上 恢复状态,请手动注入RootRestorationScope。 -
The difference between a
restorationIdand arestorationScopeId: Widgets that take arestorationScopeIdcreate a newrestorationScope(a newRestorationBucket) into which all children store their state. ArestorationIdmeans the widget (and its children) store the data in the surrounding bucket. -
restorationId与restorationScopeId的区别: 接受restorationScopeId的 widget 会创建新的restorationScope(新的RestorationBucket),所有子级在其中保存状态。restorationId表示该 widget(及其子级)在周围的 bucket 中保存数据。
恢复导航状态
#若希望应用返回到用户最近查看的特定路由(例如购物车),则还必须为导航实现状态恢复。
若直接使用 Navigator API,请将标准方法迁移为可恢复的方法(名称中包含 restorable)。例如,将 push 替换为 restorablePush。
测试状态恢复
#
要测试状态恢复,请将移动设备配置为应用进入后台后不保存状态。要了解如何在 iOS 与 Android 上操作,请参阅 RestorationManager
页面上的测试状态恢复。
其他资源
#有关状态恢复的更多信息,请参阅以下资源。
-
To learn more about short term and long term state, check out Differentiate between ephemeral state and app state.
要了解短期与长期状态的更多信息,请参阅区分临时状态与应用状态。
-
You might want to check out packages on pub.dev that perform state restoration, such as
statePersistence. -
你可能想查看 pub.dev 上执行状态恢复的 package,例如
statePersistence。 -
For more information on navigation and the
go_routerpackage, check out Navigation and routing and the State restoration topic on pub.dev.
除非另有说明,本文档之所提及适用于 Flutter 3.44.0 版本。本页面最后更新时间:2026-06-04。查看文档源码 或者 为本页面内容提出建议。