【问题标题】:AppLifcycleState.didChangeLifecycleState( )function is not called when app comes foreground or in backgroundAppLifcycleState.didChangeLifecycleState( ) 函数在应用程序进入前台或后台时不会被调用
【发布时间】:2019-01-31 14:39:04
【问题描述】:

当我打开我的应用程序或在后台运行时,不会调用 didChangeAppLifecycleState() 并且不会执行打印语句。

   import 'package:flutter/widgets.dart';
   import 'package:flutter/material.dart';


   void main(){
    runApp(
    MaterialApp(
      home: Home(),
    )
   );
   }


   class Home extends StatefulWidget {
   @override
    _HomeState createState() => _HomeState();
   }

   class _HomeState extends State<Home> with WidgetsBindingObserver{
   AppLifecycleState state;
   @override
   void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
   }
  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  void didChangeAppLifeCycleState(AppLifecycleState appLifecycleState) {
    state = appLifecycleState;
    print(appLifecycleState);
    print(":::::::");
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: Center(
          child:Text("hi")
        ),
      ),
    );
  }
}
}

didChangeAppLifeCycleState() 中的打印语句未执行。

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    这不是 OP 问题的答案,每次我遇到这个问题时,我都忘记在 initState() 中致电 WidgetsBinding.instance.addObserver(this);

    如果没有为您调用 didChangeAppLifecycleState,请不要忘记将您的实现类添加为侦听器。

    initState()dispose() 中查看 OP 的代码,并确保您也这样做。

    【讨论】:

      【解决方案2】:

      有一个错字(“Lifecycle”中的小写“c”),应该是didChangeAppLifecycleState:

      @override
      void didChangeAppLifecycleState(AppLifecycleState state) {
         state = state;
         print(state);
         print(":::::::");
      }
      

      更新:不要忘记在 initState() 中添加观察者并在 dispose() 中删除观察者

      @override
      void initState() {
        super.initState();
        WidgetsBinding.instance!.addObserver(this);
      }
      
      // ...
      
      @override
      void dispose() {
        WidgetsBinding.instance!.removeObserver(this);
        super.dispose();
      }
      

      希望对你有帮助!

      【讨论】:

      • 但这并不是真正解决 OP 问题的方法,是吗?我认为 IDE 现在很常见 :-))
      【解决方案3】:

      我必须将问题中的代码和上述 2 个答案结合起来才能得到最终结果。所以让我添加一个简单的示例来说明如何正确使用 AppLifeCycleState。

      void main() {
        runApp(MyApp());
      }
      
      class MyApp extends StatefulWidget {
         @override
         _MyAppState createState() => _MyAppState();
      }
      
      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('Resumed');
              break;
            case AppLifecycleState.inactive:
              // --
              print('Inactive');
              break;
            case AppLifecycleState.paused:
              // --
              print('Paused');
              break;
            case AppLifecycleState.detached:
              // --
              print('Detached');
              break;
          }
        }
      
        @override
        Widget build(BuildContext context) {
          return Container();
        }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-07-07
        • 1970-01-01
        • 2021-12-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多