How do I check if the Flutter application is in the foreground or not?

前端 未结 4 1401
情深已故
情深已故 2020-12-29 22:46

I don\'t want to show notification when the app is in foreground. How can I check live state of my app?

4条回答
  •  -上瘾入骨i
    2020-12-29 23:08

    To extend on the above answer, you can use a switch statement to make it nice and neat:

     @override
      void didChangeAppLifecycleState(AppLifecycleState state) {
        switch(state){
          case AppLifecycleState.resumed:
            print("app in resumed");
            break;
          case AppLifecycleState.inactive:
            print("app in inactive");
            break;
          case AppLifecycleState.paused:
            print("app in paused");
            break;
          case AppLifecycleState.detached:
            print("app in detached");
            break;
        }
    }
    

提交回复
热议问题