Flutter: how to keep the app running on background

Viewed 9964

I'm using flutter dart for developed the for foreground app to keep the app running in background on Android. It's working fine until phone is on lock screen the app is show the notification is still running but the function won't work. Is there any solution for this issues? Thanks

2 Answers

Basically you can listen to App changes with WidgetBindingsObserver https://api.flutter.dev/flutter/widgets/WidgetsBindingObserver-class.html

A good article for this (especially lockscreen) can be found here: https://medium.com/@tomalabasteruk/flutter-in-app-lock-screens-f6a17fa02af

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

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

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