question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Toolbar items are affected from previous page on Android

See original GitHub issue

Description

I have a page with three primary items and three secondary items:

image

This page is correct. But when navigating to second page, there should be two primary and two secondaries, but instead there is three primaries:

image

And the third page should have one primary and one secondary items, but both are shown as primary:

image

My guess is that the new page will have always has the same amount as primary items as the previous page.

This issue is only on Android. It works as expected on Windows at least.

Steps to Reproduce

  1. Create a new MAUI app.
  2. Update the main page:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiIssues.MainPage">

    <ContentPage.ToolbarItems>
        <ToolbarItem Text="Primary 1" />
        <ToolbarItem Text="Primary 2" />
        <ToolbarItem Text="Primary 3" />
        <ToolbarItem Text="Secondary 1"
                     Order="Secondary" />
        <ToolbarItem Text="Secondary 2"
                     Order="Secondary" />
        <ToolbarItem Text="Secondary 3"
                     Order="Secondary" />
    </ContentPage.ToolbarItems>

    <VerticalStackLayout>
        <Label 
            Text="First page. Three primary, and three secondary."
            VerticalOptions="Center" 
            HorizontalOptions="Center" />

        <Button 
            x:Name="ButtonNavigate"
            Text="Go to second page"
            Clicked="ButtonNavigate_Clicked"
            HorizontalOptions="Center" />

    </VerticalStackLayout>
</ContentPage>
public partial class MainPage : ContentPage
{
	public MainPage()
	{
		InitializeComponent();
	}

	private async void ButtonNavigate_Clicked(object sender, EventArgs e)
	{
        await Shell.Current.GoToAsync(nameof(SecondaryPage));
	}
}
  1. Add second page:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiIssues.SecondaryPage"
             Title="Second">
    <ContentPage.ToolbarItems>
        <ToolbarItem Text="Primary 1" />
        <ToolbarItem Text="Primary 2" />
        <ToolbarItem Text="Secondary 1"
                     Order="Secondary" />
        <ToolbarItem Text="Secondary 2"
                     Order="Secondary" />
    </ContentPage.ToolbarItems>
    
    <VerticalStackLayout>
        <Label 
            Text="Second page. Two primary, and two secondary."
            VerticalOptions="Center" 
            HorizontalOptions="Center" />

        <Button 
            x:Name="ButtonNavigate"
            Text="Go to third page"
            Clicked="ButtonNavigate_Clicked"
            HorizontalOptions="Center" />

    </VerticalStackLayout>
</ContentPage>
public partial class SecondaryPage : ContentPage
{
	public SecondaryPage()
	{
		InitializeComponent();
	}

	private async void ButtonNavigate_Clicked(object sender, EventArgs e)
	{
		await Shell.Current.GoToAsync(nameof(ThirdPage));
	}
}
  1. Add third page:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiIssues.ThirdPage"
             Title="Third">
    <ContentPage.ToolbarItems>
        <ToolbarItem Text="Primary 1" />
        <ToolbarItem Text="Secondary 1"
                     Order="Secondary" />
    </ContentPage.ToolbarItems>

    <VerticalStackLayout>
        <Label 
            Text="Third page. One primary, and one secondary."
            VerticalOptions="Center" 
            HorizontalOptions="Center" />
    </VerticalStackLayout>
</ContentPage>
public partial class ThirdPage : ContentPage
{
	public ThirdPage()
	{
		InitializeComponent();
	}
}

Link to public reproduction project repository

https://github.com/pekspro/MauiIssues/tree/10452_Toolbar_items_are_affected_from_previous_page

Version with bug

6.0.486 (current)

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 11

Did you find any workaround?


	public static MauiApp CreateMauiApp()
	{
		var builder = MauiApp.CreateBuilder();
		builder
			.UseMauiApp<App>()
			.ConfigureFonts(fonts =>
			{
				fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
				fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
			})
			.ConfigureMauiHandlers(handlers =>
			{
				bool fixing = false;
				ToolbarHandler.Mapper.ModifyMapping("ToolbarItems", (handler, toolbar, action) =>
				{
					if (Shell.Current is null)
					{
						action.Invoke(handler, toolbar);
						return;
					}

					if (fixing)
					{
						action?.Invoke(handler, toolbar);
					}
					else
					{
						fixing = true;
						var bar = (Shell.Current as IToolbarElement).Toolbar;
						bar.GetType().GetMethod("ApplyChanges", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy)
						.Invoke(bar, null);
						fixing = false;
					}
				});
			});

Or

Install this nuget https://www.nuget.org/packages/PureWeen.Maui.FixesAndWorkarounds

builder.ConfigureMauiWorkarounds();
/// or
builder.ConfigureShellWorkarounds();

Or

Adding the code below to an affected page does solve the problem. But there will be some flickering on navigation.

#if ANDROID
    protected override void OnNavigatedTo(NavigatedToEventArgs args)
    {
        base.OnNavigatedTo(args);

        var secondayToolBarItems = ToolbarItems.Where(x => x.Order == ToolbarItemOrder.Secondary).ToList();

        foreach (var item in secondayToolBarItems)
        {
            ToolbarItems.Remove(item);
        }

        Application.Current.Dispatcher.Dispatch(() =>
        {
            foreach (var item in secondayToolBarItems)
            {
                ToolbarItems.Add(item);
            }
        });
    }
#endif

Relevant log output

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:6
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
justcivahcommented, Dec 22, 2022

I understand the desire to prioritize certain issues, but this bug is causing significant inconvenience. Is there any way we can move this issue to a higher priority in the roadmap so it can be addressed as soon as possible?

1reaction
tataelmcommented, Mar 21, 2023

This issue exists for more than a year. https://github.com/dotnet/maui/issues/7823 and still no patch. A production ready code can’t be fully ready because of this. Please take this issue in the priority list.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Manage toolbar's navigation and back button from ...
You can use Toolbar inside the fragment and it is easy to handle. First add Toolbar to layout of the fragment <android.support.v7.widget.
Read more >
Provide custom back navigation
Back navigation is how users move backward through the history of screens they previously visited. All Android devices provide a Back button ......
Read more >
Get around on your Android phone
You can get around on your phone in different ways. Learn how to choose an option, then use that option to move between...
Read more >
issues in navigation bar back button and android ...
I am trying to override navigation bar back button and back button ... errors and codes runs but no views are displayed on...
Read more >
How To Bring Back The Android Back Button [2022] - YouTube
The Android Back Button is missing in the newer version of android, this video we don't go through how to bring the back...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found