maui
maui copied to clipboard
[Android] Toolbar item icon and text disappears when reopening from recent applications.
Description
Upon returning to foreground, previously drawn ToolbarItems of NavigationPage disappear. Looking at title shifted to the left, buttons are completely removed from layout. Debugger shows that all ToolbarItems present in IList of NavigationPage
Similar bug marked as closed, but definetely repros today (https://github.com/dotnet/maui/issues/11552)
Steps to Reproduce
- Add Navigation Page
- Add ToolbarItems in constructor of ContentPage
- Use android "back" button to put app into backgound.
- Return app to foreground
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
ToolbarItems.Add(new(null, "bug", () => { CounterBtn.Text = $"Bugt button clicked"; }));
ToolbarItems.Add(new(null, "contacts", () => { CounterBtn.Text = $"Person button clicked"; }));
}
}
Link to public reproduction project repository
https://github.com/SergTomcat/MAUI8_BugDemo2
Version with bug
8.0.80 SR8
Is this a regression from previous behavior?
Not sure, did not test other versions
Last version that worked well
Unknown/Other
Affected platforms
Android
Affected platform versions
Android 10-14
Did you find any workaround?
Override OnAppearing method of every page with toolbar, clear items and readd them.
protected override void OnAppearing()
{
base.OnAppearing();
var tb = ToolbarItems.ToArray();
ToolbarItems.Clear();
foreach (var item in tb)
ToolbarItems.Add(item);
}
It plugs the hole, but require to be included in everypage, not mentioning additional perfomance overhead.
Relevant log output
No response
Hi I'm an AI powered bot that finds similar issues based off the issue title.
Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!
Open similar issues:
- [Android] ToolbarItem shows incorrect image on different pages on initial load when used in Shell (#19950), similarity score: 0.79
Closed similar issues:
- NavigationPage.ToolbarItems not shown on Android after Back key pressed (#15096), similarity score: 0.85
- [regression/8.0.3] Navigation Toolbar broken (#18902), similarity score: 0.81
- Flyout Navigation: Android Toolbar disappears if we close app (via Back button) and re-open it. (#8266), similarity score: 0.78
- ToolbarItem icon/image doesn't update on page navigation on Android (#19673), similarity score: 0.77
Note: You can give me feedback by thumbs upping or thumbs downing this comment.
This issue has been verified using Visual Studio 17.12.0 Preview 1.0 (8.0.8). Cannot repro on Android platform.
This issue has been verified using Visual Studio 17.12.0 Preview 1.0 (8.0.8). Cannot repro on Android platform. ![bug]
Tested it on 17.12.0: if you use circle button to put app into backround, it restores normaly. But, if you use back button (◀), app restores without toolbar buttons in layout. Use Android back button. ToolbarItems still contains items, though.
https://github.com/user-attachments/assets/963a1c7e-699e-4de5-8109-ad08fb2d96e5
Thanks for the detailed information, I reproduced your problem on Visual Studio 17.12.0 Preview 2.0(8.0.90 & 8.0.80 &8.0.3)
If anyone struggles with similar issues: In my case the Android developer option "Don't keep activities" was enabled. This is a developer-only option for certain testing scenarios. Just wanted to add this as it cost me several hours to find out that this option was enabled on one of the dev phones.
The issue is still there though hard to reproduce using lightweight basic repro app. It looks like Android can delete menu buttons after switching activities and MAUI stays unaware of this and keeps using previously remembered IMenuItem list named _currentMenuItems in Toolbar.Android.cs file. I will create a PR to remove items from _currentMenuItems if they cannot be found in actual menu anymore. Everything works as expected after that.