1

When I press the device back button, it navigate to the homepage but not to the previous page.

I would like to know how to navigate to the previous page using the the device back button.

Thank you

1
  • 1
    Can you tell me how you're navigating to the current page from your previous page? It appears to me that you must be using replacement somewhere. Commented Mar 29, 2021 at 14:30

4 Answers 4

2

You might have used Navigator.pushReplacement which destroys the previous page and creates the new page.However if you use Navigator.push pressing the back button will navigate you to the previous screen.

Example:

Container(
        child: Center(
          child: TextButton(child: Text("Next page"),onPressed: (){
            Navigator.push(context, MaterialPageRoute(builder: (context) => Page2(),)); // back button will navigate to previous screen
            //Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => Page2(),)); //  back button will not navigate to previous page
          },),),),
Sign up to request clarification or add additional context in comments.

Comments

1

It was a Materialapp widget problem.

I solved the problem by deleting the Materialapp widget that was in one of the pages.

1 Comment

What a star! thanks a lot. Yes it was the same for me, I replaced Materialapp widget with direct Scaffold and all sorted.
0

This occurs because the previous navigation was not saved in the route stack.

What you should do is this:

Widget A

Navigator.push(context, WidgetB());

Inside Widget B, press the device button and you will return to widget A.

It is important that in widget A you are not using replacement or another that removes widget A from the stack.

Comments

0

You can use:

Navigator.pop(context);

or

Navigator.pushNamed(context, '/routeName');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.