Introduction: There were two articles about the life cycle of Flutter, one of which introduced the life cycle of Stateful components in Flutter.
The second part is about the platform-related life cycle of Flutter,
Blog: laomengit.com/blog/202012…
There are many more exciting articles on the Blog, and you are welcome to join the Flutter community.
The lifecycle of this article is different from the lifecycle of StatefulWidget components, where platform-specific lifecycle refers to the lifecycle of platform-specific operations, such as onPause after the Android App is retired to the background.
The video should be suspended when the App is playing a scene, return to the mobile desktop or switch to another App. AppLifecycleState is used to implement the Flutter:
class AppLifecycle extends StatefulWidget {
@override
_AppLifecycleState createState() => _AppLifecycleState();
}
class _AppLifecycleState extends State<AppLifecycle>
with WidgetsBindingObserver {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
//TODO}}@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('App Life Cycle '),
),
body: Center(
child: Text(' '),),); }@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose(); }}Copy the code
Focus is rewritten didChangeAppLifecycleState method, state of AppLifecycleState include: resumed, inactive, paused, detached.
Notification callback didChangeAppLifecycleState method is derived from the system (notifications), under normal circumstances, the App is normal to receive these notifications, but in some cases cannot be received notice, such as user forced shutdown, mobile phone without electricity automatic shutdown, etc.
The following is a detailed description of its status:
- Resumed: The application is visible and responds to user input.
- Inactive: The application is inactive and unable to respond to user input. On iOS, making a call, responding to a TouchID request, entering the app switcher, or the control Center are all in this state. On Android, split screen apps, make phone calls, pop up system dialogs or other Windows, etc.
- Pause: The application is invisible and cannot respond to user input. In this state, the engine will not call window. onBeginFrame and window. onDrawFrame.
- Detached: The application is still attached to the Flutter engine, but separated from the platform View. When in this state: The engine is first loaded in the process of attaching to a platform View, or the View is destroyed due to executing a Navigator POP.
Here are some common questions about the life cycle:
A and B have two pages, in page click the back button to return to the A, B didChangeAppLifecycleState callback
Actually the problem is most people want to achieve is similar to the features in the Android onResume, using didChangeAppLifecycleState is unable to implement this functionality, didChangeAppLifecycleState is corresponding to the entire application, Not different routes (pages) in a Flutter.
From A->B, after returning A from B, A reloads the data using the following method:
A Page code:
class A extends StatelessWidget {
@override
Widget build(BuildContext context) {
return RaisedButton(onPressed: ()async{
var result = await Navigator.of(context).push(MaterialPageRoute(builder: (context){
return B();
}));
When you return from B to A, execute the following code
//TODO loads data}); }}Copy the code
B Page code:
class B extends StatelessWidget {
@override
Widget build(BuildContext context) {
return RaisedButton(onPressed: (){
Navigator.of(context).pop('Returned parameters'); }); }}Copy the code