Skip to content

Conversation

VitalyKnyazev
Copy link
Contributor

@VitalyKnyazev VitalyKnyazev commented Apr 2, 2025

Description of Change

When updating toolbar items first removing outdated menu items as Android can sometimes delete them after switching activities (releasing resources under memory pressure?).

Issues Fixed

Fixes #24357

…can delete them after switching activities
@VitalyKnyazev VitalyKnyazev requested a review from a team as a code owner April 2, 2025 14:54
@dotnet-policy-service dotnet-policy-service bot added the community ✨ Community Contribution label Apr 2, 2025
@VitalyKnyazev VitalyKnyazev changed the title Removing outdated menu items as Android can delete them after switching activities [Android] Removing outdated menu items as Android can delete them after switching activities Apr 3, 2025
Copy link
Contributor

@jsuarezruiz jsuarezruiz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we include a test? Can move App to background etc using Appium using App.BackgroundApp().

var clonedPreviousMenuItems = previousMenuItems.ToArray();
foreach (var previousMenuItem in clonedPreviousMenuItems)
{
var item = menu.FindItem(previousMenuItem.ItemId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can directly: if (menu.FindItem(previousMenuItem.ItemId) == null)

// menu items can be deleted by Android after switching activities, removing outdated menu items first
if (menu != null)
{
var clonedPreviousMenuItems = previousMenuItems.ToArray();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can reduce the memory allocation and copying overhead of ToArray() iterating backwards through the collection.

for (int i = previousMenuItems.Count - 1; i >= 0; i--)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @jsuarezruiz, thank you for the suggestions, updated PR.
I have never added tests like this and creating test environment will take forever, not sure I can do that.

@jsuarezruiz
Copy link
Contributor

/azp run MAUI-UITests-public

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

if (menu.FindItem(previousMenuItem.ItemId) == null)
{
previousMenuItem.Dispose();
previousMenuItems.Remove(previousMenuItem);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

previousMenuItems.RemoveAt(j);

Use RemoveAt(j) instead of Remove(previousMenuItem). Remove() which has to search the collection for the item while RemoveAt(j) directly removes at the index we already know, eliminating the search operation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, done

@jsuarezruiz
Copy link
Contributor

/azp run MAUI-UITests-public

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Copy link
Contributor

@jsuarezruiz jsuarezruiz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we include a test here? Can create an UITest based on the issue sample and use the methods

public static void BackgroundApp(this IApp app)
to move the app to background and then to foreground.

Let me know if can help with something!

@jsuarezruiz
Copy link
Contributor

Could we include a test here? Can create an UITest based on the issue sample and use the methods

public static void BackgroundApp(this IApp app)

to move the app to background and then to foreground.
Let me know if can help with something!

@VitalyKnyazev Would you like to include the tests yourself? If you don’t have time, just let me know, I’d be happy to add them for you!

@VitalyKnyazev
Copy link
Contributor Author

Hi @jsuarezruiz, I have tried App.BackgroundApp but still getting the toolbar item via App.WaitForElement(AppiumQuery.ByXPath("//android.widget.Button[@content-desc='ToolbarItem1']")).
It is not always possible to reproduce the issue, I could only reliably do it while debugging my app which is quite heavyweight.

@PureWeen PureWeen changed the base branch from main to inflight/current June 24, 2025 17:22
@PureWeen PureWeen merged commit beb7c53 into dotnet:inflight/current Jun 24, 2025
79 checks passed
PureWeen pushed a commit that referenced this pull request Jun 25, 2025
…er switching activities (#28767)

* Fixing issue #24357 by removing outdated menu items as Android can delete them after switching activities

* Disposing outdated IMenuItem just in case

* Reduced memory allocation

* Tuned performance a bit
github-actions bot pushed a commit that referenced this pull request Jun 26, 2025
…er switching activities (#28767)

* Fixing issue #24357 by removing outdated menu items as Android can delete them after switching activities

* Disposing outdated IMenuItem just in case

* Reduced memory allocation

* Tuned performance a bit
PureWeen pushed a commit that referenced this pull request Jun 27, 2025
…er switching activities (#28767)

* Fixing issue #24357 by removing outdated menu items as Android can delete them after switching activities

* Disposing outdated IMenuItem just in case

* Reduced memory allocation

* Tuned performance a bit
PureWeen pushed a commit that referenced this pull request Jun 28, 2025
…er switching activities (#28767)

* Fixing issue #24357 by removing outdated menu items as Android can delete them after switching activities

* Disposing outdated IMenuItem just in case

* Reduced memory allocation

* Tuned performance a bit
github-actions bot pushed a commit that referenced this pull request Jun 29, 2025
…er switching activities (#28767)

* Fixing issue #24357 by removing outdated menu items as Android can delete them after switching activities

* Disposing outdated IMenuItem just in case

* Reduced memory allocation

* Tuned performance a bit
github-actions bot pushed a commit that referenced this pull request Jun 29, 2025
…er switching activities (#28767)

* Fixing issue #24357 by removing outdated menu items as Android can delete them after switching activities

* Disposing outdated IMenuItem just in case

* Reduced memory allocation

* Tuned performance a bit
github-actions bot pushed a commit that referenced this pull request Jul 1, 2025
…er switching activities (#28767)

* Fixing issue #24357 by removing outdated menu items as Android can delete them after switching activities

* Disposing outdated IMenuItem just in case

* Reduced memory allocation

* Tuned performance a bit
github-actions bot pushed a commit that referenced this pull request Jul 2, 2025
…er switching activities (#28767)

* Fixing issue #24357 by removing outdated menu items as Android can delete them after switching activities

* Disposing outdated IMenuItem just in case

* Reduced memory allocation

* Tuned performance a bit
github-actions bot pushed a commit that referenced this pull request Jul 2, 2025
…er switching activities (#28767)

* Fixing issue #24357 by removing outdated menu items as Android can delete them after switching activities

* Disposing outdated IMenuItem just in case

* Reduced memory allocation

* Tuned performance a bit
github-actions bot pushed a commit that referenced this pull request Jul 8, 2025
…er switching activities (#28767)

* Fixing issue #24357 by removing outdated menu items as Android can delete them after switching activities

* Disposing outdated IMenuItem just in case

* Reduced memory allocation

* Tuned performance a bit
github-actions bot pushed a commit that referenced this pull request Jul 8, 2025
…er switching activities (#28767)

* Fixing issue #24357 by removing outdated menu items as Android can delete them after switching activities

* Disposing outdated IMenuItem just in case

* Reduced memory allocation

* Tuned performance a bit
github-actions bot pushed a commit that referenced this pull request Jul 9, 2025
…er switching activities (#28767)

* Fixing issue #24357 by removing outdated menu items as Android can delete them after switching activities

* Disposing outdated IMenuItem just in case

* Reduced memory allocation

* Tuned performance a bit
@github-actions github-actions bot locked and limited conversation to collaborators Jul 25, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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