Life cycle in flutter

前端 未结 8 825
予麋鹿
予麋鹿 2020-12-07 10:48

Does flutter have a method like Activity.resume() which can tell developer the user has gone back to the activity.

When I pick the data from internet in

8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-07 11:28

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

    In 1st page (when navigating to 2nd page)

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

    In 2nd page (when navigating back to 1st page)

    Navigator.pop(context, returnedValue);
    

    Lifecycle callback

    void main() => runApp(HomePage());
    
    class HomePage extends StatefulWidget {
      @override
      _HomePageState createState() => _HomePageState();
    }
    
    class _HomePageState extends State 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();
      }
    }
    

提交回复
热议问题