initState() method of first activity is not initializing after taking popback stack from the aother activity
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),
.
Kishan sharma
Updated on December 28, 2022Comments
-
Kishan sharma over 1 year
I am working on flutter application where I am using 3 screens.
MainActivity
open toActivityClass1
andActivityClass1
will openActivityClass2
. Now when I am takingNavigator.pop(context),
from ActivityClass2 it will shows ActivityClass1 from the stack. But this time I need to takeinitState()
ofActivityClass1
as I need to refresh few data onActivityClass1
.Is there any way to call
initState()
ofActivityClass1
which also maintain my stack forMainActivity
->ActivityClass1
? I have also tried forNavigator.of(context).pushAndRemoveUntil(
instead of usingNavigator.pop(context),
onActivityClass2
but this will clears my stack. -
Kishan sharma about 3 yearsI have tried the same solution but this can create a stack like MainActivity() -> Screen1 -> Screen1. So now when I press back from screen1 it will pop again to Screen1.
-
Kishan sharma about 3 yearsIf I will use the
Navigator.push
call again then this screen will also be added to my stack, so now if I'll press back this will show me like Screen1 -> Screen2 -> Screen1 -> MainActivity. Which is the wrong flow. I need something where it works as screen1 -> MainActivity in my stack after pressing back from screen2. -
Tasnuva Tavasum oshin about 3 yearsUse pushReplacement