Skip to content

Any view's Loaded event is called when navigating away from the page #25156

Open

Description

Description

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        MyGrid.Loaded += async (_, _) => { await Shell.Current.DisplayAlert("Loaded", "Loaded", "OK"); };
        MyGrid.Unloaded += async (_, _) => { await Shell.Current.DisplayAlert("Unloaded", "Unloaded", "OK"); };
    }
    
    private async void OnCounterClicked(object sender, EventArgs e)
    {
        await Shell.Current.GoToAsync(nameof(TestPage1));
    }
}

When I navigate into this MainPage, Loaded event is called once, as expected.
When I navigate away from the page by clicking the button, Loaded event is called again, and Unloaded event is call called twice.

I've got an action that is only required to be run on actual navigation into the page (I cannot use Appearing due to the view not being loaded at that point). So the question is, how can I avoid loaded being called when navigating away from the page? If this is a bug, is there a practical way for the function running for the loaded event to tell if I am navigating in or out? So I can ignore it if it is out.

Steps to Reproduce

  1. Create a brand new project with two pages
  2. Add above code to the main page
  3. Run the app and try to navigate away from the page

Note, it is not a Grid specific issue, same problem with StackLayouts, Labels etc.

Link to public reproduction project repository

No response

Version with bug

8.0.91 SR9.1

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

No response

Affected platforms

iOS

Affected platform versions

iOS 17

Did you find any workaround?

No response

Relevant log output

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    area-controls-shellShell Navigation, Routes, Tabs, Flyoutarea-navigationNavigationPageplatform/iOS 🍎s/triagedIssue has been revieweds/verifiedVerified / Reproducible Issue ready for Engineering Triaget/bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions