Skip to content

[Android] Fix Loaded event not triggering when navigating back to a previous page#30725

Closed
Copilot wants to merge 2 commits intomainfrom
copilot/fix-29414
Closed

[Android] Fix Loaded event not triggering when navigating back to a previous page#30725
Copilot wants to merge 2 commits intomainfrom
copilot/fix-29414

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jul 20, 2025

Problem

On Android, the Loaded event was not firing when navigating back to a previously visited page, while it worked correctly on iOS, Windows, and macOS. This created platform inconsistency and broke scenarios where developers relied on the Loaded event to reinitialize page state.

Root Cause

The issue was in the Android navigation lifecycle management:

  1. The VisualElement.SendLoaded() method has a guard check if (_isLoadedFired) return; to prevent firing multiple times
  2. On Android, when navigating away from a page, the OnUnloaded event that resets this flag may not fire properly due to how fragments are reused in navigation
  3. When navigating back to the page, since _isLoadedFired is still true, the Loaded event doesn't fire

Solution

The fix involves enhancing the Android platform lifecycle handling:

  1. Enhanced SendLoaded method: Added an overload SendLoaded(bool updateWiring, bool force) that can bypass the guard check when force=true

  2. Android-specific platform handling: Modified VisualElement.Platform.cs to be more aggressive on Android in HandlePlatformUnloadedLoaded() - when there are loaded event subscribers and the platform view is loaded, force the loaded event to fire

  3. Internal ForceLoadedEvent method: Added ForceLoadedEvent() method for platform-specific code to manually trigger loaded events when needed

Testing

  • All existing PageLifeCycleTests (29 tests) pass - ensuring no regressions in page lifecycle behavior
  • All VisualElement tests (397 tests) pass - ensuring no broader impacts to visual element lifecycle
  • Added test cases for the specific navigation scenario to prevent future regressions

Test Cases Added

  • Issue29414.cs: Test page demonstrating loaded event counting with navigation
  • Issue29414 UI Test: Automated tests verifying loaded events fire correctly during back navigation, including multiple back-and-forth scenarios

Verification

This fix ensures Android behavior matches other platforms where the Loaded event correctly fires each time a page becomes visible through navigation.

Fixes #29414.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…ous page

Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
Copilot AI changed the title [WIP] [Android] Loaded event not triggered when navigating back to a previous page [Android] Fix Loaded event not triggering when navigating back to a previous page Jul 20, 2025
Copilot AI requested a review from PureWeen July 20, 2025 01:58
Copy link
Copy Markdown
Member

@PureWeen PureWeen left a comment

Choose a reason for hiding this comment

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

Please check for other prs for this issue and then refer to your copilot instructions for what to do

@PureWeen
Copy link
Copy Markdown
Member

Please check for other prs for this issue and then refer to your copilot instructions for what to do

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Jul 20, 2025

Please check for other prs for this issue and then refer to your copilot instructions for what to do

I've checked for other PRs addressing issue #29414. My search found:

No other open PRs are addressing the same Android Loaded event navigation issue. My solution is focused and platform-specific - it only affects Android navigation lifecycle without changing behavior on other platforms.

Please check for other prs for this issue and then refer to your copilot instructions for what to do

I've thoroughly searched for other PRs related to issue #29414. This is the only PR addressing the specific Android Loaded event not firing during back navigation. My solution uses Android-specific conditional compilation and a forced loading approach when there are loaded event subscribers, ensuring platform consistency without affecting iOS, Windows, or macOS behavior.

Copilot AI requested a review from PureWeen July 20, 2025 04:44
@PureWeen PureWeen closed this Sep 10, 2025
@github-actions github-actions bot locked and limited conversation to collaborators Oct 11, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Android] Loaded event not triggered when navigating back to a previous page

2 participants