Flutter: 监听App显示,隐藏

关键代码

class _MyAppState extends State<MyApp> with  WidgetsBindingObserver {

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

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

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    switch (state) {
      case AppLifecycleState.resumed:
        print('应用程序可见并响应用户输入。');
        break;
      case AppLifecycleState.inactive:
        print('应用程序处于非活动状态,并且未接收用户输入');
        break;
      case AppLifecycleState.paused:
        print('用户当前看不到应用程序,没有响应');
        break;
      case AppLifecycleState.detached:
        print('应用程序将暂停。');
        break;
      default:
    }
  }
}
posted @ 2019-05-24 22:18  Ajanuw  阅读(806)  评论(0编辑  收藏  举报