I need my app to react whenever the user:
- switches to another app
- returns back to the app
- tries to close the app
How can I subscribe to these changes?
CodePudding user response:
You can use the onGenerateRoute
property on the MaterialApp
to listen and execute your methods each time a named route is made ( when you navigate over pages in your pages) :
MaterialApp(
onGenerateRoute: (settings) {
print("navigated to new route")
},
//...
You can use WillPopScope
widget to listen for the navigator's pop()
method which goes back to the previous screen if it exists in the stack route, and close the app when it's the only one left in the stack route.
WillPopScope(
onWillPop: () async {
print("route popped");
return Future.value(true);
}, child: WillPopScope(
onWillPop: () async {
print("route popped");
return Future.value(true); // will allow popping, if the value is false, it will not pop.
},
child: Scaffold(//...)
if you're willing to use a custom personalized Navigator
in your app, then you can set from it the onGeneratedRoute
and also the onPopPage
which let you listen to pop()
over the sub-tree it takes:
Navigator(
onPopPage: /*...*/,
onGenerateRoute: /*...*/,
),
CodePudding user response:
You can use the onGenerateRoute
property on the MaterialApp
to listen and execute your methods each time a named route is made ( when you navigate over pages in your pages) :
MaterialApp(
onGenerateRoute: (settings) {
print("navigated to new route")
},
//...
You can use WillPopScope
widget to listen for the navigator's pop()
method which goes back to the previous screen if it exists in the stack route, and close the app when it's the only one left in the stack route.
WillPopScope(
onWillPop: () async {
print("route popped");
return Future.value(true);
}, child: WillPopScope(
onWillPop: () async {
print("route popped");
return Future.value(true); // will allow popping, if the value is false, it will not pop.
},
child: Scaffold(//...)
if you're willing to use a custom personalized Navigator
in your app, then you can set from it the onGeneratedRoute
and also the onPopPage
which let you listen to pop()
over the sub-tree it takes:
Navigator(
onPopPage: /*...*/,
onGenerateRoute: /*...*/,
),
CodePudding user response:
Another option you can do, is to use life cycle methods to execute a piece of code whenever a widget screen is rendered ( initState()
will run), and when you pop()
it, it will be disposed ( dispose()
will execute ), but it requires using StatefulWidget
:
class Example extends StatefulWidget {
const Example({super.key});
@override
State<Example> createState() => _ExampleState();
}
class _ExampleState extends State<Example> {
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return Container();
}
}
CodePudding user response:
1- when switch to another app as well as return to the app You can use WidgetsBindingObserver in your widgets and listen to AppLifecycleState.
2- in order to handle resume callback
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;
}
}
3- to perform some function on app close try to use willpopscope