Flutter 应用中有Activity种生命周期方法吗?

喜欢:

onCreate()
onResume()
onDestroy()

或者:

viewDidload()
viewWillAppear()

如何处理应用生命周期?

推荐答案

当系统将应用程序放在后台或将应用程序返回到名为didChangeAppLifecycleState的前台时,会调用一个方法.

Example with widgets:

  class _AppLifecycleReactorState extends State<AppLifecycleReactor> with WidgetsBindingObserver {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  AppLifecycleState _notification;

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    setState(() { _notification = state; });
  }

  @override
  Widget build(BuildContext context) {
    return new Text('Last notification: $_notification');
  }
}

还有100种方法可以了解应用程序可能处于的状态,例如:

  1. 不活动
  2. 已暂停
  3. 已恢复
  4. 暂停

这些常量的用法是常量的值,例如:

const AppLifecycleState(state)

Flutter相关问答推荐

如何使用底部导航栏修复此导航问题

使用ThemeProvider不在亮模式和暗模式之间切换

CMakeLists.txt处的CMake错误:3(项目)找不到指定的Visual Studio实例

Flutter 翼future 建造者不断开火和重建.它在每次重建时都会生成一个新的随机数--我如何防止这种情况?

Flutter:无法构建iOS应用程序-找不到PigeonParser.h文件

如何在Dart中允许正则表达式中有一个空格,但允许其他字符为1或更多?

如何在flutter中将所有单词的第一个字母大写

构建上下文不能跨异步间隙使用

Flutter http 请求获取 null

无法应用插件com.chaquo.python

Riverpod 注册多少个 providers

Firebase Auth - 有没有办法为新用户预先生成 UID?

Flutter可滚动定位列表支持rtl方向吗?

如何在 flutter 中使用 tabbar 作为底部导航栏?

Flutter Websockets MacOS:相同的代码在调试模式下有效,但在发布模式下无效:(操作系统错误:提供了 node 名或服务名..)

如何在 flutter 的alert 对话框中实现进度指示器?

有什么方法可以将 ChangeNotifier 与 ValueListenableBuilder 一起使用

Flutter:整个判断整个应用生命周期的通用类

如何将图像网络装箱到具有边界半径的容器中

RangeError(索引):无效值:有效值范围为空:点击文章时为0