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?
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 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 ?
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
Currently Im using native_admob_flutter for my flutter app. I also included AppOpenAds and Interstital ad within the app.
I want to display the AppOpen ad when user open the app/ resume to the app, here is how I do it in main.dart:
#override
Future<void> didChangeAppLifecycleState(AppLifecycleState state) async {
switch (state) {
case AppLifecycleState.resumed:
loadAndDisplayAppOpenAd();
break;
case AppLifecycleState.inactive:
break;
case AppLifecycleState.paused:
break;
case AppLifecycleState.detached:
break;
}
}
However I found that displaying Interstial ad will also change the app state -> inactive -> paused -> resume, which trigger AppOpen ad.
This really create bad user experinence. Is there a way to overcome this? Thank you!
You may need a variable to make sure that app has been paused by user action, and not by interstitial.
static bool pausedByInterstitial = false;
...
class AppLifecycleReactor extends WidgetsBindingObserver {
final AppOpenAdManager appOpenAdManager;
AppLifecycleReactor({required this.appOpenAdManager});
static bool pausedByInterstitial = false;
#override
Future<void> didChangeAppLifecycleState(AppLifecycleState state) async {
if (state == AppLifecycleState.resumed && !pausedByInterstitial) {
appOpenAdManager.showAdIfAvailable();
}
}
}
...
showInter() {
AppLifecycleReactor.pausedByInterstitial = true;
InterstitialListener(
...
onAdHiddenCallback: (ad) {
AppLifecycleReactor.pausedByInterstitial = false;
},
...
);
}
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
}))
);
}
...
}