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.
Start App.
Expected: Toolbar item "About" should be present
Actual: No toolbar item is present
Navigate to second page via button
Navigate back via toolbar. This works and "OnOptionsItemSelected()" is triggered.
Expected and Actual: Toolbar item "About" is now shown on MainPage and is working.
Version with issue: 2.5.0.280555
Last known good version:
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...
Most helpful comment
Encountered the same problem as well.
Worked around it by calling this in the OnAppearing of the first page: