MichelPeseur
MichelPeseur

Reputation: 187

Why does absolute navigation URL not clear navigation stack?

While I was investigating on an issue that makes my app staying on splash screen even after navigating to another page, I found out something that seems weird to me.

I managed to reproduce the issue in a small and simple project. Basically I have 3 pages and ViewModels in my Prism project: SplashPage (ContentPage), MainPage (ContentPage), RootPage (MasterDetailPage).

They are all registered for navigation, as well as the NavigationPage.

Here is what I have in my App.xaml.cs:

protected override async void OnInitialized()
{
     InitializeComponent();
     await NavigationService.NavigateAsync("/SplashPage");
}

And in the SplashPageViewModel's OnNavigatedTo override:

public override async void OnNavigatedTo(INavigationParameters parameters)
{
    Debug.WriteLine($"URI: {NavigationService.GetNavigationUriPath()}");
    await NavigationService.NavigateAsync("/RootPage/NavigationPage/MainPage");
    Debug.WriteLine($"URI: {NavigationService.GetNavigationUriPath()}");     
}

And here is the output:

[0:] URI: /SplashPage
[0:] URI: /RootPage/NavigationPage/MainPage/SplashPage?useModalNavigation=true

I feel very confused because I was thinking that any navigation request that begins with "/" should clear the navigation stack. So why does the SplashPage remain ?

Upvotes: 2

Views: 1444

Answers (2)

Hamid Shaikh
Hamid Shaikh

Reputation: 197

For Clearing Navigation Stack

Use Abosulute Uri like below to clear all navigation stack

await NavigationService.NavigateAsync(new Uri("app:///RootPage/NavigationPage/MainPage", UriKind.Absolute));

In your case seems you only want splashscreen page to be removed from the stack then you can simply change like below.

await NavigationService.NavigateAsync("../RootPage/NavigationPage/MainPage");

Note:- The above syntax will clear SplashScreen from the NavigationStack (Also, this will work if you are using prism.forms 7+ version)

Upvotes: 0

Ranga
Ranga

Reputation: 1108

You've got an interesting problem!!. I've never tried navigating in OnNavigatedTo

You might want to talk to Brian Lagunas about the internal workings of OnNavigatedTo or may be take a look at the repo

After reading you're question, I was able to reproduce the same behavior. Taking it a step further I've added some delay in calling the MainPage and it worked just fine.

public override async void OnNavigatedTo(INavigationParameters parameters)
{
    Debug.WriteLine($"URI: {NavigationService.GetNavigationUriPath()}");
    Task.Run(async() => {
    await Task.Delay(2000);
    Device.BeginInvokeOnMainThread(async () => {
        await NavigationService.NavigateAsync("/RootPage/NavigationPage/MainPage");
    }});
    Debug.WriteLine($"URI: {NavigationService.GetNavigationUriPath()}");     
}

The output in console is still the same, but when i print the Navigation page in MainPage OnNavigatedTo it is showing correctly! and the UI/Navigation stack doesn't have the SplashPage

Beware: Its a hack and I strongly suggest against calling Task.Run unnecessarily

Upvotes: 2

Related Questions