Assuming that you are using a stateful widget add a widgetbinding observer to it
class _yourClassState extends State<YourClass>
with WidgetsBindingObserver {
// Here you can override initstate, build etc
}
Now widgetBindingObserver will also let you override didChangeAppLifeCycleState which will allow you to capture the state of the app.
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) {
case AppLifecycleState.resumed:
print("app in resumed from background"); //you can add your codes here
break;
case AppLifecycleState.inactive:
print("app is in inactive state");
break;
case AppLifecycleState.paused:
print("app is in paused state");
break;
case AppLifecycleState.detached:
print("app has been removed");
break;
}
}