Life cycle in awe

Does flutter have a method like Activity.resume() that can tell the developer that the user has returned to action.

When I select data from the Internet on page B and return to page A, how can I tell page A that the data is prepared.

+26
source share
6 answers
  1. createState (): when the Framework is instructed to create a StatefulWidget, it immediately calls createState ()

  2. mount is true: when createState creates your state class, buildContext is assigned to this state. BuildContext is an overly simplified place in the widget tree that hosts this widget. Here is a longer explanation. All widgets have the bool this.mounting property. It becomes true when buildContext is assigned. Error calling setState when widget is disabled.

  3. initState (): this is the first method called when creating the widget (of course, after the constructor of the class). initState is called once and only once. It should be called super.initState ().

  4. didChangeDependencies (): this method is called immediately after initState when the widget is first built.

  5. build (): this method is called frequently. This is necessary, and he must return the widget.

  6. didUpdateWidget (Widget oldWidget): if the parent widget changes and needs to rebuild this widget (because it needs to provide other data to it), but it rebuilds with the same runtimeType, then this method is called. This is because Flutter reuses a state that has lived for a long time. In this case, you can initialize some data again, as in initState.

  7. setState (): this method is often called from the framework itself and from the developer. Used to notify the structure of data changes.

  8. deactivate (): Deactivation is called when State is removed from the tree, but can be reinserted until the current frame change is completed. This method exists mainly because state objects can be moved from one point in the tree to another.

  9. dispose (): Dispose is called when a State object that is persistent is deleted. This method is used to unsubscribe and cancel all animations, streams, etc.

  10. mount is false: the state object can never be remounted, and an error is thrown, setState is called.

+29
source

Here is an example here: https://github.com/flutter/flutter/blob/master/examples/layers/services/lifecycle.dart

You need to use WidgetsBindingObserver

+15
source

Application life cycle

For LifeCycle you need to use WidgetsBindingObserver , it works when the application comes to the foreground and background.

 import 'package:flutter/widgets.dart'; class YourWidgetState extends State<YourWidget> with WidgetsBindingObserver { @override void initState() { WidgetsBinding.instance.addObserver(this); super.initState(); } @override void dispose() { WidgetsBinding.instance.removeObserver(this); super.dispose(); } @override void didChangeAppLifecycleState(AppLifecycleState state) { if (state == AppLifecycleState.resumed) { //do your stuff } } } 

But in my case, I could not catch the OnResume situation when switching from one screen to another. therefore, the code below works similarly to startActivityForResult .

use this code when moving to another activity

 Navigator.push(context, MaterialPageRoute(builder: (context) => ProductDetails(pid: productList[index]["pid"],), settings: RouteSettings(name: '/productdetail')),).then((value){ setState(() { length=value; }); debugPrint('CHECK BACK FROM DETAIL $length'); }); 

when pressed back

 onPressed: (){Navigator.pop(context,length);} 
+4
source

I don't think flutter app lifecycle callbacks will help you here. You can try this logic.

On the 1st page (when switching to the 2nd page)

 Navigator.push(context, MaterialPageRoute(builder: (context) => Page2())).then((value) { print("Value returned form Page 2 = $value"); }; 

On page 2 (when switching to page 1)

 Navigator.pop(context, returnedValue); 

Callback Lifecycle

 void main() => runApp(HomePage()); class HomePage extends StatefulWidget { @override _HomePageState createState() => _HomePageState(); } class _HomePageState extends State<HomePage> with WidgetsBindingObserver { @override void initState() { super.initState(); WidgetsBinding.instance.addObserver(this); } @override void didChangeAppLifecycleState(AppLifecycleState state) { super.didChangeAppLifecycleState(state); print("Current state = $state"); } @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text("Lifecycle")), body: Center(child: Text("Center"),), ), ); } @override void dispose() { WidgetsBinding.instance.removeObserver(this); super.dispose(); } } 
+1
source

here you can find out how accurately you can return data from "Activity -B" back to "Activity A"

0
source

enter image description here Constructor

This function is not part of the life cycle, because this time the state of the widget property is empty, if you want to access the widget properties in the constructor, it will not work. But the constructor should be the first call.

createState

When Flutter is instructed to build a StatefulWidget , it immediately calls createState()

Initial state

Called when this object is inserted into the tree.

When a render tree is inserted during a call, this function is called only once in the life cycle. Here you can perform some initialization, such as initialization state variables.

Setstate

The setState() method is often called from the Flutter environment itself and from the developer.

didChangeDependencies

Called when the dependency of this object [State] changes.

didUpdateWidget

Called when the widget configuration is changed.

deactivate

Called when this object is removed from the tree. Before disposing, we will call this function.

dispose of

Called when this object is permanently removed from the tree.

didChangeAppLifecycleState

Called when the system puts the application in the background or returns the application to the foreground.

Here is a nice detailed document: https://flutterbyexample.com/stateful-widget-lifecycle/

  import 'package:flutter/material.dart'; class ScreenLifecyle extends StatefulWidget { ScreenLifecyleState state; //createState(): When the Framework is instructed to build a StatefulWidget, it immediately calls createState() @override State<StatefulWidget> createState() { // TODO: implement createState return ScreenLifecyleState(); } } class ScreenLifecyleState extends State<ScreenLifecyle> { /* mounted is true: When createState creates your state class, a buildContext is assigned to that state. BuildContext is, overly simplified, the place in the widget tree in which this widget is placed. Here a longer explanation. All widgets have a bool this.mounted property. It is turned true when the buildContext is assigned. It is an error to call setState when a widget is unmounted. mounted is false: The state object can never remount, and an error is thrown is setState is called. */ /* This is the first method called when the widget is created (after the class constructor, of course.) initState is called once and only once. It must called super.initState(). */ @override void initState() { // TODO: implement initState super.initState(); print("initState"); } /* This method is called immediately after initState on the first time the widget is built. */ @override void didChangeDependencies() { // TODO: implement didChangeDependencies super.didChangeDependencies(); print("didChangeDependencies"); } /* build(): This method is called often. It is required, and it must return a Widget. */ @override Widget build(BuildContext context) { print("build"); // TODO: implement build return Container(); } /* If the parent widget changes and has to rebuild this widget (because it needs to give it different data), but it being rebuilt with the same runtimeType, then this method is called. This is because Flutter is re-using the state, which is long lived. In this case, you may want to initialize some data again, as you would in initState. */ @override void didUpdateWidget(ScreenLifecyle oldWidget) { print("didUpdateWidget"); // TODO: implement didUpdateWidget super.didUpdateWidget(oldWidget); } @override void setState(fn) { print("setState"); // TODO: implement setState super.setState(fn); } /* Deactivate is called when State is removed from the tree, but it might be reinserted before the current frame change is finished. This method exists basically because State objects can be moved from one point in a tree to another. */ @override void deactivate() { // TODO: implement deactivate print("deactivate"); super.deactivate(); } /* Dispose is called when the State object is removed, which is permanent. This method is where you should unsubscribe and cancel all animations, streams, etc. */ @override void dispose() { // TODO: implement dispose super.dispose(); } @override void didChangeAppLifecycleState(AppLifecycleState state) { super.didChangeAppLifecycleState(state); switch (state) { case AppLifecycleState.inactive: print('appLifeCycleState inactive'); break; case AppLifecycleState.resumed: print('appLifeCycleState resumed'); break; case AppLifecycleState.paused: print('appLifeCycleState paused'); break; case AppLifecycleState.suspending: print('appLifeCycleState suspending'); break; } } } 
0
source

Source: https://habr.com/ru/post/1262336/


All Articles