How do I check if the Flutter application is in the foreground or not?
Geofencing
A sample geofencing plugin with background execution support for Flutter.
Getting Started
This plugin works on both Android and iOS. Follow the instructions in the following sections for the platforms which are to be targeted.
Android
Add the following lines to your AndroidManifest.xml
to register the background service for geofencing:
<receiver android:name="io.flutter.plugins.geofencing.GeofencingBroadcastReceiver" android:enabled="true" android:exported="true"/> <service android:name="io.flutter.plugins.geofencing.GeofencingService" android:permission="android.permission.BIND_JOB_SERVICE" android:exported="true"/>
Also request the correct permissions for geofencing:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
Geofencing
A sample geofencing plugin with background execution support for Flutter.
Getting Started
This plugin works on both Android and iOS. Follow the instructions in the following sections for the platforms which are to be targeted.
Android
Add the following lines to your AndroidManifest.xml
to register the background service for geofencing:
<receiver android:name="io.flutter.plugins.geofencing.GeofencingBroadcastReceiver" android:enabled="true" android:exported="true"/> <service android:name="io.flutter.plugins.geofencing.GeofencingService" android:permission="android.permission.BIND_JOB_SERVICE" android:exported="true"/>
Also request the correct permissions for geofencing:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
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
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.
share improve this answer follow
answered Oct 19 '18 at 10:55
1,79311 gold badge1414 silver badges2525 bronze badges
-
2
Do I need to add WidgetsBindingObserver in all screen widget classes or only in a widget which is attached to the main class? – Ameer Nov 21 '19 at 9:13
-
1
You need to add with WidgetsBindingObserver to the widgets in which you want to do some reaction on AppLyfecycle events – Valentina Konyukhova Dec 19 '19 at 13:32
-
The example is working as expected but I also get a message when the app is resumed: E/BpSurfaceComposerClient(10905): Failed to transact (-1). What may be the cause ? – cwhisperer Mar 18 at 9:51
-
1
You can always use enums with this as well (recommended) AppLifecycleState.resumed for example – Oliver Dixon Jul 17 at 12:38
-
Can we asume that the initial state is resumed? – Diego Garcia Oct 8 at 17:56
24
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;
}
}
share improve this answer follow
answered May 28 at 20:32
33833 silver badges1919 bronze badges
-
1
you forgot to close curly braces at the end. Please update the answer. Thanks. – Kamlesh Oct 29 at 7:49
1
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();
}
share improve this answer follow
answered Nov 10 at 10:13
80.1k2121 gold badges266266 silver badges197197 bronze badges
0
My Answer may be late but may be helpful for someone. I also faced this problem there is a plugin which works on both android and ios
https://pub.dev/packages/flutter_app_lock
Where you can put you desired lockscreen also it is very flexible.
Hope someone who stuck like me can get this easily done.
推荐阅读
-
How do I check if a type is a subtype OR the type of an object?
-
How do I safely use direct IO while forking in a multi-threaded application?
-
How do I check if the Flutter application is in the foreground or not?
-
How do I deploy a Tizen application to the Tizen emulator
-
How do I create an IIS application and application pool using InnoSetup script
-
How do I deploy a Flask application in IIS?