Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

flutter

dart

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

like image 631
Abc Xyz Avatar asked Aug 14 '18 06:08

Abc Xyz


People also ask

How do I start the foreground in Flutter?

Open the AndroidManifest. xml file and specify it between the <manifest> and <application> tags. And we need to add this permission to automatically resume foreground service at boot time. And specify the service inside the <application> tag as follows.

What is foreground service Flutter?

foreground service is either for running long running tasks. that is why i named it android long task. or running a task non stop. now you can run foreground service non stop. and have your code run on a periodic basis.

Is Flutter considered front end?

Flutter is a popular frontend development framework from Google that enables developers to build beautiful frontends for any screen. Flutter is designed to streamline cross-platform app development while maintaining a consistent user experience.


4 Answers

In your State<...> class you need to implement WidgetsBindingObserver interface and listen for widget state changes. Something like this:

class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver {
  AppLifecycleState? _notification; 
  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    setState(() {
      _notification = state;
    });
  }

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

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

Then when you want to know what is the state, check

 _notification.index property. _notification == null => no state changes happened, 
0 - resumed, 
1 - inactive, 
2 - paused.
like image 84
Valentina Konyukhova Avatar answered Oct 14 '22 07:10

Valentina Konyukhova


To extend on @CopsOnRoad's 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;
    }
}
like image 38
crushman Avatar answered Oct 14 '22 06:10

crushman


Simply create a bool variable which will keep track of all your background/foreground stuff.

Full code:

class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
  // This variable will tell you whether the application is in foreground or not. 
  bool _isInForeground = true;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance!.addObserver(this);
  }
  
  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    _isInForeground = state == AppLifecycleState.resumed;
  }

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

  @override
  Widget build(BuildContext context) => Scaffold();
}
like image 31
CopsOnRoad Avatar answered Oct 14 '22 05:10

CopsOnRoad


Also there is a package named flutter_fgbg for this.

Example:

FGBGNotifier(
    onEvent: (event) {
        print(event); // FGBGType.foreground or FGBGType.background
    },
    child: ...,
)

Or:

subscription = FGBGEvents.stream.listen((event) {
    print(event); // FGBGType.foreground or FGBGType.background
});

// in dispose
subscription.cancel();

Why:

Flutter has WidgetsBindingObserver to get notified when app changes its state from active to inactive states and back. But it actually includes the state changes of the embedding Activity/ViewController as well. So if you have a plugin that opens a new activity/view controller(eg: image picker) or in iOS if you start a FaceID prompt then WidgetsBindingObserver will report as the app is inactive/resumed.

This plugin on the other hand reports the events only at app level. Since most apps need only background/foreground events this plugin is implemented with just those events. In iOS, plugin reports didEnterBackgroundNotification and willEnterForegroundNotification notifications and in Android, plugin reports these using androidx.lifecycle:lifecycle-process package.

Checkout example/ project to see the differences in action.

Example link.

like image 20
Omid Raha Avatar answered Oct 14 '22 06:10

Omid Raha