Search code examples
flutterflutter-layoutflutter-state

initState() method of first activity is not initializing after taking popback stack from the aother activity


I am working on flutter application where I am using 3 screens.

MainActivity open to ActivityClass1 and ActivityClass1 will open ActivityClass2. Now when I am taking Navigator.pop(context), from ActivityClass2 it will shows ActivityClass1 from the stack. But this time I need to take initState() of ActivityClass1 as I need to refresh few data on ActivityClass1.

Is there any way to call initState() of ActivityClass1 which also maintain my stack for MainActivity -> ActivityClass1? I have also tried for Navigator.of(context).pushAndRemoveUntil( instead of using Navigator.pop(context), on ActivityClass2 but this will clears my stack.


Solution

  • Navigator.push returns a Future when the MaterialPageRoute passed into it is resolved. You could await that Future to know when some other view has popped back to it.

    Example,

    Navigator.of(context).push(MaterialPageRoute(
      builder: 
        (context) => NewView(),
      ),
    ).then((_) {
      // Call setState() here or handle this appropriately
    });
    

    So now when I am coming back to my Activity Class1 this method will help to take any action while comming back to screen by just using Navigator.pop(context),.