I would simply like to know if there is any performances issue by using in multiple widgets the WidgetsBindingObserver and more specifically, the call to :
#override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
switch (state) {
case AppLifecycleState.resumed:
break;
case AppLifecycleState.inactive:
break;
case AppLifecycleState.paused:
break;
case AppLifecycleState.detached:
break;
}
}
Is it the same listener for the whole or is it memory consuming for multiple listeners added to multiple places ?
Related
We need some help with the flutter app badge.
We are completing a chat app in Flutter that runs on Firebase.
We want the app to reset its badge to Zero when a user checked all the unread messages and minimized the app. Currently, the reset is not happening. This is our screenplay.
Our Screenplay
We use flutter_app_badger with this code:
#override
void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) {
case AppLifecycleState.resumed:
log("MyApp: AppLifecycaeState.resumed");
FlutterAppBadger.removeBadge();
break;
case AppLifecycleState.paused:
updateMyActiveStatus(false);
log("MyApp: AppLifecycleState.paused");
break;
case AppLifecycleState.inactive:
log("MyApp: AppLifecycleState.inactive");
break;
case AppLifecycleState.detached:
log("MyApp:
AppLifecycleState.detached");
break;
default:
break;
}
We also tried this code:
#override
void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) {
case AppLifecycleState.resumed:
log("MyApp: AppLifecycaeState.resumed");
break;
case AppLifecycleState.paused:
updateMyActiveStatus(false);
log("MyApp: AppLifecycleState.paused");
FlutterAppBadger.removeBadge();
break;
case AppLifecycleState.inactive:
log("MyApp: AppLifecycleState.inactive");
break;
case AppLifecycleState.detached: l
log("MyApp: AppLifecycleState.detached");
break;
default:
break;
}
Neither seems to work.
We read that the Flutter App Badger is not fully compatible with all Android devices but it should work with Samsung, HTC and many Android brands.
In our testing, we use Samsung.
I have a StatefulWidget and at the beginning of the State method's build I do
WidgetsBinding.instance
.addObserver(LifecycleEventHandler(resumeCallBack: () async {
if (mounted) {
refreshState();
}
}));
This is the code of the LifecycleEventHandler
class LifecycleEventHandler extends WidgetsBindingObserver {
final AsyncCallback resumeCallBack;
LifecycleEventHandler({
this.resumeCallBack,
});
#override
Future<void> didChangeAppLifecycleState(AppLifecycleState state) async {
switch (state) {
case AppLifecycleState.resumed:
if (resumeCallBack != null) {
await resumeCallBack();
}
break;
case AppLifecycleState.inactive:
break;
case AppLifecycleState.paused:
break;
case AppLifecycleState.detached:
break;
}
}
}
the resumeCallBack is being called every time the app is resumed, even if the page is not visible to the user (if I navigate to another page without removing this from the stack).
Is there a way to run this callback only if the page is visible to the user? I don't want to dispose the state/page but just avoid unnecessary state updates
Is it possible to manually control the flutter AppLifeCycleState?
#override
void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) {
case AppLifecycleState.resumed:
debugPrint("Colorun - resumed");
break;
case AppLifecycleState.inactive:
debugPrint("Colorun - inactive");
break;
case AppLifecycleState.paused:
debugPrint("Colorun - paused");
break;
case AppLifecycleState.detached:
debugPrint("Colorun - detached");
break;
}
}
Is it possible to change the flutter app from running state to paused state?
hai I need to call some function when app will reopen from sleep
after user can put app in minimize then when the reopen the app that function have to call in flutter
You need to listen to lifecycle events like this
class LifecycleEventHandler extends WidgetsBindingObserver {
final AsyncCallback resumeCallBack;
final AsyncCallback suspendingCallBack;
LifecycleEventHandler({
this.resumeCallBack,
this.suspendingCallBack,
});
#override
Future<void> didChangeAppLifecycleState(AppLifecycleState state) async {
switch (state) {
case AppLifecycleState.resumed:
if (resumeCallBack != null) {
await resumeCallBack();
}
break;
case AppLifecycleState.inactive:
case AppLifecycleState.paused:
case AppLifecycleState.detached:
if (suspendingCallBack != null) {
await suspendingCallBack();
}
break;
}
}
}
class AppWidgetState extends State<AppWidget> {
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(
LifecycleEventHandler(resumeCallBack: () async => setState(() {
// here the app has resumed
}))
);
}
...
}
I implemented the WidgetsBindingObserver, but the app is NEVER sent to the background so it doesn't recognize the AppLifecycleState.resumed
this is the current implementation
#override
void didChangeAppLifecycleState(AppLifecycleState state) async {
print('\n\ndidChangeAppLifecycleState');
switch (state) {
case AppLifecycleState.resumed:
print('\n\nresumed');
_mymethod();
break;
case AppLifecycleState.inactive:
print('\n\ninactive');
break;
case AppLifecycleState.paused:
print('\n\npaused');
break;
case AppLifecycleState.detached:
print('\n\ndetached');
break;
}
}
to simulate the process i do the next in android
run the project as --release
open the widget with the WidgetsBindingObserver
open another app (like chrome or phone settings)
return to the app
when returning to the app i can see my widget on screen, the app doesn't restart, but NONE of the prints appears on the console not event the print('\n\ndidChangeAppLifecycleState'); and _mymethod(); is never executed
The WidgetsBindingObserver mixin requires a bit more work than merely implementing the interface. You also need to add the following to your widget state class:
#override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
#override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}