Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve crash when navigating to PopupPage after app is sent to background #150

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

MathiasLopezNareia
Copy link

This is a fix for a crash that occurs on Android when navigating to a PopupPage under specific conditions, such as when the application is sent to the background while a task is still running.

Steps to Reproduce:

  1. Use a button to trigger navigation after a delay.
  2. After pressing the button, send the app to the background.
  3. Simulate a delay before navigating.
  4. After the delay, the root navigation is reset by assigning a new NavigationPage to App.Current.MainPage, navigating to a page that, in its OnAppearing method, triggers a navigation to a PopupPage.

Example:

mainStackLayout.Add(new Button()
{
    Text = "MainPage2",
    Command = new AsyncCommand(async () =>
    {
        await Task.Delay(3000); // Allow time to send the app to the background
        Console.WriteLine("Navigating to page 2");
        App.Current.MainPage = new NavigationPage(new MainPage2());
    })
});

Cause of the Crash:

  • The crash occurs in the AddAsync method of the AndroidMopups class when page.Parent.Handler is null. Specifically, this happens while attempting to create a new PopupPageHandler instance using page.Parent.Handler.MauiContext, which results in a null reference exception.

Solution:

  1. Introduced a FindMauiContext extension method to traverse the visual tree and retrieve a valid MauiContext if page.Parent.Handler.MauiContext is null.
  2. Updated the AddAsync method to use this method to ensure a valid context is available before creating the PopupPageHandler.

Related Issue:

This fix also resolves a similar issue described in Issue #118

Pushing a Popup shortly after starting the App leads to a NullReferenceException, and the Popup does not get pushed. On Android, the app crashes sometimes in this situation, but only in release mode.

The changes in this PR address the root cause of this issue by ensuring a valid MauiContext is available during navigation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant