[Android] Fix Loaded event not triggering when navigating back to a previous page#30725
[Android] Fix Loaded event not triggering when navigating back to a previous page#30725
Conversation
…ous page Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
PureWeen
left a comment
There was a problem hiding this comment.
Please check for other prs for this issue and then refer to your copilot instructions for what to do
|
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.
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. |
Problem
On Android, the
Loadedevent 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 theLoadedevent to reinitialize page state.Root Cause
The issue was in the Android navigation lifecycle management:
VisualElement.SendLoaded()method has a guard checkif (_isLoadedFired) return;to prevent firing multiple timesOnUnloadedevent that resets this flag may not fire properly due to how fragments are reused in navigation_isLoadedFiredis stilltrue, the Loaded event doesn't fireSolution
The fix involves enhancing the Android platform lifecycle handling:
Enhanced SendLoaded method: Added an overload
SendLoaded(bool updateWiring, bool force)that can bypass the guard check whenforce=trueAndroid-specific platform handling: Modified
VisualElement.Platform.csto be more aggressive on Android inHandlePlatformUnloadedLoaded()- when there are loaded event subscribers and the platform view is loaded, force the loaded event to fireInternal ForceLoadedEvent method: Added
ForceLoadedEvent()method for platform-specific code to manually trigger loaded events when neededTesting
Test Cases Added
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.