Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear Navigation in Xamarin/MAUI?

Tags:

xamarin

maui

I have the following app structure:

Main Page : Choose Login Type A or B

if the user choses login type A then navigate to > Page A1 Credentials for A type
if the user choses login type B then navigate to > Page B1 Credentials for B type

if Login is successful for page A1 navigate to > Page A2
if Login is successful for page B1 navigate to > Page B2


            Page A1 --- Login Successful ---> Page A2
          /
         /
        /
Page 1  \
         \
          \
            Page B1 --- Login Successful ---> Page B2

If the user is in Page A1 or B1, I would like to allow him to go back to page 1 if he wants. However if the user logs in either using type 1 or 2 and reaches either A2 or B2, he shouldn't be allowed to go back.

Here is my code:

Before going to pages A2 or B2 I run. (This code executes in pages A1, B1):

public static void RemoveAllPagesFromNavigation(INavigation Navigation)
    {
        var existingPages = Navigation.NavigationStack.ToList();
        foreach (var page in existingPages)
        {
            if (page != null)
            {
                Navigation.RemovePage(page);
            }
        }
    }

RemoveAllPagesFromNavigation(this.Navigation);
await Navigation.PushAsync(new PageA2());       // Or PageB2

But the function RemoveAllPagesFromNavigation raises an exception saying that the first element of the NavigationStack is null;

I added a condition to skip the case where the first page is null, the function runs but the end user is still able to go back to page Page 1.

How can I prevent user from going back to the pages 1, A1 or B2?

like image 257
Thomas Carlton Avatar asked Dec 14 '25 23:12

Thomas Carlton


1 Answers

According to Shell navigation / Absolute routes, the use of // at beginning of a route will replace the current nav stack. Thus there will be nowhere to go back to.

await Shell.Current.GoToAsync("//Page2");

FYI ALTERNATIVE:

There is an alternative navigation paradigm, NavigationPage that lacks appshell features such as "Routes". Advantage is simple, direct, programmatic control over the navigation stack. (Without the subtleties that AppShell introduces. One of which you have encountered.)

Consider using NavigationPage during the login sequence, only going to AppShell once user is in your main content. (Or omit AppShell completely, just use NavigationPage for your app.)

To do so, in App.xaml.cs, replace:
MainPage = new AppShell();
with:
MainPage = new NavigationPage();.

Then use NavigationPage features to show first login page, etc. Don't use anything from that AppShell navigation doc I linked earlier.

If after login success, you want to use AppShell navigation, do:
Application.Current.MainPage = new AppShell();.


THIRD ALTERNATIVE:

Do Application.Current.MainPage = new MyNextFascinatingPage(); to step through the login sequence. Thus there is nowhere for user to go back to, during these steps. (If you want them to go back, you do so explicitly, by creating a new copy of earlier page.)

After login success, do:
Application.Current.MainPage = new AppShell();.

like image 149
ToolmakerSteve Avatar answered Dec 16 '25 22:12

ToolmakerSteve



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!