maui icon indicating copy to clipboard operation
maui copied to clipboard

[Android] Toolbar item icon and text disappears when reopening from recent applications.

Open SergTomcat opened this issue 1 year ago • 3 comments

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

  1. Add Navigation Page
  2. Add ToolbarItems in constructor of ContentPage
  3. Use android "back" button to put app into backgound.
  4. 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

SergTomcat avatar Aug 21 '24 14:08 SergTomcat

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:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

github-actions[bot] avatar Aug 21 '24 14:08 github-actions[bot]

This issue has been verified using Visual Studio 17.12.0 Preview 1.0 (8.0.8). Cannot repro on Android platform. bug

ninachen03 avatar Aug 22 '24 08:08 ninachen03

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

SergTomcat avatar Aug 22 '24 12:08 SergTomcat

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) icon

ninachen03 avatar Sep 20 '24 10:09 ninachen03

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.

thomasgalliker avatar Nov 25 '24 15:11 thomasgalliker

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.

VitalyKnyazev avatar Apr 02 '25 14:04 VitalyKnyazev