-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Fix Android crashing issue #4860
Conversation
@@ -66,6 +63,9 @@ void IApplication.CloseWindow(IWindow window) | |||
|
|||
internal void RemoveWindow(Window window) | |||
{ | |||
if (_pendingMainPage != null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will cause the window to never be removed I think (down in line 87).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like that's fine
If you're not overriding CreateWindow
then we're keeping around a _pendingMainPage
that will always be used.
So, it seems like there's really no point in creating/recreating the xplat window.
It seems like the _pendingMainPage
and the window it inhabits should just always exist
Basically this PR is just disabling all XXXXWindow
behavior if you set MainPage
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will leave the window in the visual tree though, I'm not sure that's ideal.
Also concerned about any lifecycle related things this might not fire as a result?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because it still is in the logical visual tree. If you're only using APIs that support a single window then your single window that you get never goes away. It's the same thing as how your _pendingMainPage
is reused. There's never a scenario that follows this path where you would want to close
a window. These events will only fire if you background the app or if you click the back button. So this path only runs when the user isn't looking at the app. Once they go back to the app then the same window is used.
Also concerned about any lifecycle related things this might not fire as a result?
We don't tie any lifecycle events to the xplat window being removed from the App's logical children
If we're going to remove the window then we should also force the user to recreate MainPage
if we're going to retain the same MainPage
then it makes sense to retain the same window
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not opposed to forcing a new main page in this instance. The right thing to do here is follow what android is doing which is destroying the activity and recreating it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Redth that makes Android incongruent with WinUI/iOS though. Because this is a very specific to Android scenario that we are working around so that if someone is 17 pages deep on a NavigationPage
it doesn't completely reset their application if they get a phone call or a txt message
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@PureWeen let's back up a second here then as maybe there's conflating issues. Just switching to another app (ie, hit home button, or task switcher) shouldn't usually cause the activity to be destroyed. Opening a notification to a text probably shouldn't trigger the activity to be destroyed. If that's happening we need to investigate that, maybe we're missing a configuration change being handled by default.
What I'm advocating for here is that if the activity is actually destroyed we clear out the page because that is just like a window being closed.
Now, the back button I believe is a special case. I think that may cause the activity to be destroyed by default, but I think we might want to consider overriding OnBackPressed
and calling MoveTaskToBack(true)
or the C# api equivalent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just switching to another app (ie, hit home button, or task switcher) shouldn't usually cause the activity to be destroyed.
If the device is low end enough I'm pretty sure it does. If you take a really low end device and background your app the activity will get destroyed.
That's what this setting in "Developer options" is for testing
For example you can test for this inside android as well
https://developer.android.com/guide/components/activities/testing#recreate
If a device is low on resources, the system might destroy an activity, requiring your app to recreate that activity when the user returns to your app. To simulate these conditions, call recreate():
If a user is using the API
App.MainPage = new MainPage()
There's never a scenario where the intention is to "close" your window because when you're using the above API you're only allowed one window. The code in this PR is basically mimicking how iOS/Windows work. iOS/Windows will never kill it's respective platform window out from under you.
Now, if the user is overriding CreateWindow
that's a a different story. Now the user is in charge of the windows creation themselves so they can decide what to do when the OS wants to create a new window. That scenario isn't changed by this PR.
This PR is purely for the case where the user doesn't even know that we added Window
to .NET MAUI they just want to use App.MainPage = new MainPage()
as if nothing changed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still disagree 😆 however I think it's probably safe enough to leave this to be an implementation detail we can revisit later and merge this workaround for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/azp run |
Azure Pipelines successfully started running 2 pipeline(s). |
Description of Change
Fixes #4169
Android templates app is crashing when starting up after backing out because MainPage, being set from App and not from CreateWindow, was getting cleared
PR Checklist
Does this PR touch anything that might affect accessibility?
If any of the above checkboxes apply to your PR, then the PR will need to provide testing to demonstrate that accessibility still works.