2

I have a MAUI Shell app that I'm working on using a TabBar. I have a header that I have created that will show on multiple tabs and direct users to a notifications page.

If the user clicks the header link it routes to the notifications page fine. The issue is if the user clicks on the currently active tab to go back to that nothing happens. I'm trying to capture an OnClick Event or something from the Tab to go back to the root or clear the notification page.

I found some people trying to solve this in Xamarin with custom renderers but I was wondering if anyone has run into or solved this issue in MAUI.

For clarification the issue I have is on a tab the user clicks a link gets routed to a new page, but clicking on that active tab again does not go back to that tab.

I cannot find an event trigger in order to add some logic to force navigation back to a tab or a page refresh.

AppShell.xaml.cs

public AppShell()
{
    InitializeComponent();

    Routing.RegisterRoute("Notifications", typeof(Notifications));
    Routing.RegisterRoute("home", typeof(MainPage));
}

AppShell.Xaml:

<TabBar>
    <Tab Title="{x:Static resources:Lang.Home}" Icon="{StaticResource HomeTab}" >
        <ShellContent Route="main" ContentTemplate="{DataTemplate local:MainPage}" />
    </Tab>

    <Tab Title="{x:Static resources:Lang.Shop}" Icon="shop.png" CurrentItem="{x:Reference partSearchPage}">
        <ShellContent Title="{x:Static resources:Lang.SearchInventory}" x:Name="inventorySearchPage" Route="inventorysearch"  ContentTemplate="{DataTemplate inv:InventorySearch}" />
    </Tab>

    <Tab Title="{x:Static resources:Lang.Cart}" Icon="{StaticResource CartTab}">
        <ShellContent Route="cart" ContentTemplate="{DataTemplate cart:CartPage}" />
    </Tab>
</TabBar>

HeaderBar.xaml.cs

private async void NotificationsClicked(object sender, EventArgs args)
{
    await Shell.Current.GoToAsync("Notifications");
}
Julian
  • 5,290
  • 1
  • 17
  • 40
Luke Konecki
  • 129
  • 1
  • 1
  • 8
  • Can you clarify the problem? I don't quite understand what exactly you're trying to do. – Julian Apr 18 '23 at 18:18
  • 1
    @ewerspej added to question, basically need to trigger an event when a tab is click regardless of if it is the active tab or not. – Luke Konecki Apr 18 '23 at 19:53
  • I believe you're still leaving out important parts of the code. If I understand correctly, you have a couple of Tabs (as shown in your code) and you also navigate to other Views which are not part of the AppShell. How do you navigate to those Views? – Julian Apr 18 '23 at 21:12
  • Do you mean that you clicked the navigation button then it can make a navigation to the new page but it can not navigate back? – Guangyu Bai - MSFT Apr 19 '23 at 07:24
  • If I am on the home tab it is active, I click on the notification icon which then does the GoToAsync and loads that page fine. The home tab however is still active and if I click on that again nothing happens. I would expect it to navigate back to the home page. I can use the device back button to go back but just seems strange the home tab click does not to anything when active. – Luke Konecki Apr 19 '23 at 13:21
  • You can check this answer about [Reset Navigation on TabBar Item Click](https://stackoverflow.com/a/73924870/19335715). – Guangyu Bai - MSFT May 17 '23 at 09:13
  • The suggestions miss the essential sentence: *"if the user clicks on the **currently active tab** ... nothing happens"*. There is no event (because that tab is already active). Thus, there is no place to put code that will clear the detail page (here, notification page) from the nav stack, to get back to root page of that active tab. Any fix would require writing a custom handler. – ToolmakerSteve May 26 '23 at 18:50

1 Answers1

0

I walked into this a while ago. It seems that this is case for Android only. Because on my IOS device, everything works as expected.

You can cycle over:

Shell.Current.Navigation.NavigationStack

Removing every page, starting from the last and skipping the first.

You can check this answer: https://stackoverflow.com/a/73924870/6643940

I do not do it exactly like that, but it is very good place to start.

H.A.H.
  • 2,104
  • 1
  • 8
  • 21