Search code examples
c#mvvmmaui

MAUI: What is the difference between GoToAsync() and PushAsync() to navigate between pages?


I am trying to learn how is the best way to navigate between pages in a MAUI application and MVVM.

I have seen that in the examples from Microsoft, the default way to do it is with PushAsync(), but later I have seen that it is possible to navigate with Shell.Current.GoToAsync().

Which is the difference between both? Or are they complementary?


Solution

  • I have created a sample to test the PushAsync() and GoToAsync().

    The GoToAsync() navigates between the current page and the target page according to the route. So it will not create multiple instances of the same page. One page only have one instance. And if you navigate such as Page1 -> Page2 with the GoToAsync(), you can't use the GoToAsync() go to Page1. You can only use GoToAsync("..") go to Page1.

    But for the PushAsync(), you can use the PushAsync(new Page2()) to create multiple instance of the same page. And the navigation stack will be like Page1 -> Page2 -> Page1 -> Page2...

    So the GoToAsync() is like the PushAsync() with the singleton page. And you need to register the route when you use the GoToAsync().

    In addition, when you use GoToAsync() to navigate, there are some limits, such as you can pass a collection data to the next page. But when you use the PushAsync(), you can pass almost all types of the data to target page.

    Generally speaking, the PushAsync() is easy to use and pass data and the GoToAsync() has the better function but needs more details.