Skip to content

Conversation

@kubaflo
Copy link
Contributor

@kubaflo kubaflo commented Apr 5, 2025

Issues Fixed

Fixes #28811

Before After
Screen.Recording.2025-04-05.at.02.58.12.mov
Screen.Recording.2025-04-05.at.03.00.04.mov

CC @pictos

Copilot AI review requested due to automatic review settings April 5, 2025 01:03
@kubaflo kubaflo requested a review from a team as a code owner April 5, 2025 01:03
@kubaflo kubaflo requested review from mattleibow and rmarinho April 5, 2025 01:03
@kubaflo kubaflo self-assigned this Apr 5, 2025
@dotnet-policy-service dotnet-policy-service bot added the community ✨ Community Contribution label Apr 5, 2025
@dotnet-policy-service
Copy link
Contributor

Hey there @@kubaflo! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

if (!preventBackPropagation)
{
customComponentDialog.OnBackPressedDispatcher.OnBackPressed();
_ = _modalNavigationManager?.PopModalAsync(true);
Copy link

Copilot AI Apr 5, 2025

Choose a reason for hiding this comment

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

Ensure that _modalNavigationManager is always non-null when invoked in HandleOnBackPressed. If there's any possibility for a null value, consider adding appropriate fallback handling or logging to maintain robust modal navigation behavior.

Copilot uses AI. Check for mistakes.
@kubaflo kubaflo added platform/android area-navigation NavigationPage area-controls-modal and removed community ✨ Community Contribution labels Apr 5, 2025
WeakReference<CustomComponentDialog> _customComponentDialog;

public CallBack(bool enabled, CustomComponentDialog customComponentDialog) : base(enabled)
private ModalNavigationManager? _modalNavigationManager;
Copy link
Contributor

Choose a reason for hiding this comment

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

just because this CallBack is a Java.Lang.Object, can we store the ModalNavigationManager as a WeakReference?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point! Done

@jsuarezruiz
Copy link
Contributor

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

jsuarezruiz
jsuarezruiz previously approved these changes Apr 7, 2025
App.WaitForElement("NavigateToDetailPage");
App.Click("NavigateToDetailPage");
App.WaitForElement("Issue28811DetailPage");
App.Back();
Copy link
Contributor

@bhavanesh2001 bhavanesh2001 Apr 7, 2025

Choose a reason for hiding this comment

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

Maybe this test should be only for Android?

image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yea probably, thanks!

@rmarinho rmarinho added this to the .NET 9 SR7 milestone Apr 8, 2025
@rmarinho rmarinho moved this from Todo to Changes Requested in MAUI SDK Ongoing Apr 8, 2025
Copy link
Member

@rmarinho rmarinho left a comment

Choose a reason for hiding this comment

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

OverridingBackButtonShouldNotCauseStackOverflow failing on iOS and Android, also please rebase :)

@kubaflo
Copy link
Contributor Author

kubaflo commented Apr 14, 2025

Can you please /azp again?

@PureWeen PureWeen modified the milestones: .NET 9 SR7, .NET 9 SR8 May 8, 2025
@PureWeen
Copy link
Member

PureWeen commented May 8, 2025

/rebase

@PureWeen PureWeen moved this from Changes Requested to Ready To Review in MAUI SDK Ongoing May 8, 2025
@PureWeen
Copy link
Member

PureWeen commented May 8, 2025

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@PureWeen PureWeen modified the milestones: .NET 9 SR8, .NET 9 SR9 Jun 2, 2025
@PureWeen PureWeen modified the milestones: .NET 9 SR9, .NET 9 SR10 Jul 3, 2025
@PureWeen PureWeen modified the milestones: .NET 9 SR10, .NET 9 SR12 Aug 4, 2025
@PureWeen
Copy link
Member

/rebase

@PureWeen
Copy link
Member

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

{
customComponentDialog.OnBackPressedDispatcher.OnBackPressed();
{
_ = mnm.PopModalAsync(true);
Copy link
Member

Choose a reason for hiding this comment

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

@kubaflo this one is a little interesting because the original author is returning false but they are opting out of all of our navigation logic

The fix here also feels incorrect because we are only running this test on android.

My first thought with how this would work is that if the user is overriding OnBackButotnPResed on page and they don't call the base class and then they return false

That by default we don't do anything at that point.

If they are opting out of calling our base class and returning false we can't really assume what to do for them at this point.

@pictos do you remember why we opted to call the OnBackPresed from the Dispatcher here?

Copy link
Contributor

Choose a reason for hiding this comment

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

The Dispatcher is from AndroidX, just for reference. I think this was implemented to enable modal pages to listen for and handle back button presses. We register the ComponentDialog here, and when the callback is triggered, we forward that event into Maui's structure.

Copy link
Member

Choose a reason for hiding this comment

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

yea, but this is what causes the infinite loop

Like, if I'm following the code right I feel like the fix here should just be deleting this code

						{
							customComponentDialog.OnBackPressedDispatcher.OnBackPressed();
						}

this just causes HandleBackPressed to get called infinitely when you return false

image

Copy link
Member

Choose a reason for hiding this comment

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

@kubaflo I think the fix here is to just delete this code completely

if (!preventBackPropagation && _modalNavigationManager.TryGetTarget(out var mnm))
						{
							customComponentDialog.OnBackPressedDispatcher.OnBackPressed();
						}

This whole situation is a bit of a mess :-/

I don't think the "OnBackButtonPressed" was fully thought through with Modal or Android really as we moved to MAUI

like, currently with a navigation page if you return false it'll cause a double back navigation and exit the app.

Technically from the API description I think the way you'd use this API would be more like this

		protected override bool OnBackButtonPressed()
		{
			if (mycustomLogic)
			{
				return true;
			}
			else
			{
				return base.OnBackButtonPressed();
			}
		}

I also worry about this change because if users call "base.OnBackButtonPressed" I think that's going to cause a double navigation back since the base class will also navigate back

@github-project-automation github-project-automation bot moved this from Ready To Review to Changes Requested in MAUI SDK Ongoing Aug 20, 2025
Eliminated the preventBackPropagation variable and related event handler code from ModalNavigationManager.Android.cs, simplifying the OnBackPressed lifecycle event handling.
@github-project-automation github-project-automation bot moved this from Changes Requested to Approved in MAUI SDK Ongoing Aug 21, 2025
@PureWeen PureWeen changed the base branch from main to inflight/current August 21, 2025 10:53
@PureWeen PureWeen merged commit 14685b2 into dotnet:inflight/current Aug 21, 2025
1 check passed
@github-project-automation github-project-automation bot moved this from Approved to Done in MAUI SDK Ongoing Aug 21, 2025
github-actions bot pushed a commit that referenced this pull request Aug 22, 2025
…ed returning false in a modally pushed page causes stack overflow - fix (#28812)

* Update ModalNavigationManager.Android.cs

[Android] ModalNavigationManager

* Update ModalNavigationManager.Android.cs

* Remove unused back propagation logic in Android modal manager

Eliminated the preventBackPropagation variable and related event handler code from ModalNavigationManager.Android.cs, simplifying the OnBackPressed lifecycle event handling.
jfversluis pushed a commit that referenced this pull request Aug 25, 2025
…ed returning false in a modally pushed page causes stack overflow - fix (#28812)

* Update ModalNavigationManager.Android.cs

[Android] ModalNavigationManager

* Update ModalNavigationManager.Android.cs

* Remove unused back propagation logic in Android modal manager

Eliminated the preventBackPropagation variable and related event handler code from ModalNavigationManager.Android.cs, simplifying the OnBackPressed lifecycle event handling.
github-actions bot pushed a commit that referenced this pull request Sep 4, 2025
…ed returning false in a modally pushed page causes stack overflow - fix (#28812)

* Update ModalNavigationManager.Android.cs

[Android] ModalNavigationManager

* Update ModalNavigationManager.Android.cs

* Remove unused back propagation logic in Android modal manager

Eliminated the preventBackPropagation variable and related event handler code from ModalNavigationManager.Android.cs, simplifying the OnBackPressed lifecycle event handling.
github-actions bot pushed a commit that referenced this pull request Sep 5, 2025
…ed returning false in a modally pushed page causes stack overflow - fix (#28812)

* Update ModalNavigationManager.Android.cs

[Android] ModalNavigationManager

* Update ModalNavigationManager.Android.cs

* Remove unused back propagation logic in Android modal manager

Eliminated the preventBackPropagation variable and related event handler code from ModalNavigationManager.Android.cs, simplifying the OnBackPressed lifecycle event handling.
@github-actions github-actions bot locked and limited conversation to collaborators Sep 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[Android] Overriding back button functionality with OnBackButtonPressed returning false in a modally pushed page causes stack overflow

6 participants