Xamarin.forms: Toolbar button not shown on App startup

Created on 16 Mar 2018  路  8Comments  路  Source: xamarin/Xamarin.Forms

Description

On Android "SetSupportActionBar()" has to be set to be able to intercept "OnOptionsItemSelected()". When not setting "SetSupportActionBar()" the function "OnOptionsItemSelected()" in MainActivity will not be triggered.

Unfortunately after adding this line of code the toolbar item is not shown at the first page after startup (MainPage). The toolbar item will appear after navigating to a different page and back to MainPage.

Steps to Reproduce

  1. Start App.
    Expected: Toolbar item "About" should be present
    Actual: No toolbar item is present

  2. Navigate to second page via button

  3. Navigate back via toolbar. This works and "OnOptionsItemSelected()" is triggered.
    Expected and Actual: Toolbar item "About" is now shown on MainPage and is working.

Basic Information

  • Version with issue: 2.5.0.280555

  • Last known good version:

  • IDE:
  • Platform Target Frameworks:

    • Android: 8.1 (but bug was in earlier releases, too

  • Android Support Library Version: 27.0.2
  • Affected Devices: Tested on Samsung Galaxy Tab S2 (Android 7.0), Google Pixel (Android 8.0)

Reproduction Link

https://github.com/TheSprigg/Toolbarbutton_not_shown/

5 help wanted high impact Android bug up-for-grabs

Most helpful comment

Encountered the same problem as well.

Worked around it by calling this in the OnAppearing of the first page:

/// <summary>
/// Workaround for ToolbarItems not apearing on android (https://github.com/xamarin/Xamarin.Forms/issues/2118)
/// </summary>
private void FixToolBarItemsNotAppearingOnAndroid()
{
    if (Device.RuntimePlatform == Device.Android)
    {
        Task.Run(async () =>
        {
            await Task.Delay(1000);
            var items = ToolbarItems.ToList();
            Device.BeginInvokeOnMainThread(() =>
            {
                ToolbarItems.Clear();
                foreach (var item in items)
                {
                    ToolbarItems.Add(item);
                }
            });
        });
    }
}

All 8 comments

I have a pseudosplashscreen for that matter as a workaround.
Funny thing is i'm doing som heavy workload with it, so it is actually good to have it now.

But this even happens on very rare occaions with the xaml ToolbarItem aswell, i never could reproduce it though...
And to be honest, alot issues i've encountered with Xamarin are posted here as bugs months later. :)

I'm struggling the same problem, I tried different ways to fix, but all my tries without success.

Just hit this one as well. @davidW83 can you expand on how you implemented your workaround?

Encountered the same problem as well.

Worked around it by calling this in the OnAppearing of the first page:

/// <summary>
/// Workaround for ToolbarItems not apearing on android (https://github.com/xamarin/Xamarin.Forms/issues/2118)
/// </summary>
private void FixToolBarItemsNotAppearingOnAndroid()
{
    if (Device.RuntimePlatform == Device.Android)
    {
        Task.Run(async () =>
        {
            await Task.Delay(1000);
            var items = ToolbarItems.ToList();
            Device.BeginInvokeOnMainThread(() =>
            {
                ToolbarItems.Clear();
                foreach (var item in items)
                {
                    ToolbarItems.Add(item);
                }
            });
        });
    }
}

This doesn't appear to be fixed in Xamarin.Forms v3.2.0.xxxx.
Does we have a timeline for addressing this? Thanks

I have run into this issue with version 4.5.0.530, I will try the work around posted above.

Are there any other ways to be able to handle OnOptionsItemSelected without having the toolbar items disappear?

The work around posted above seems to work, I have made it an extension method as I have potentially a number of different initial screens, which all include 1 or more toolbar items :-

    /// <summary>
    /// Workaround for ToolbarItems not appearing on android (https://github.com/xamarin/Xamarin.Forms/issues/2118)
    /// </summary>
    public static void FixToolBarItemsNotAppearingOnAndroid(this Page page)
    {
        if (Device.RuntimePlatform == Device.Android)
        {
            Task.Run(async () =>
            {
                await Task.Delay(1000);
                var items = page.ToolbarItems.ToList();
                Device.BeginInvokeOnMainThread(() =>
                {
                    page.ToolbarItems.Clear();
                    foreach (var item in items)
                    {
                        page.ToolbarItems.Add(item);
                    }
                });
            });
        }
    }

Encountered the same problem as well.

Worked around it by calling this in the OnAppearing of the first page:

/// <summary>
/// Workaround for ToolbarItems not apearing on android (https://github.com/xamarin/Xamarin.Forms/issues/2118)
/// </summary>
private void FixToolBarItemsNotAppearingOnAndroid()
{
    if (Device.RuntimePlatform == Device.Android)
    {
        Task.Run(async () =>
        {
            await Task.Delay(1000);
            var items = ToolbarItems.ToList();
            Device.BeginInvokeOnMainThread(() =>
            {
                ToolbarItems.Clear();
                foreach (var item in items)
                {
                    ToolbarItems.Add(item);
                }
            });
        });
    }
}

That hadn't worked for me, I had to increase the time significantly... But now you can see that the button will be added afterwards...

Was this page helpful?
0 / 5 - 0 ratings