but doing that results in closing both pages
Yes, this is just the case as you said.
But you can try the following workaround.
The idea is that we can first pop up the existing modal page(Let's say it's SecondPage), and once coming back to the previous page(Let's say it's MainPage), then we can push a new one modal page(Let's say it's ThirdPage) on MainPage. And we use EventHandler to handle the event coming back to MainPage from SecondPage.
Please refer to the following code:
MainPage.xaml.cs
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
SecondPage.mEventHander += delegate (object s, int value) {
BackCall(s, value);
};
}
private async void BackCall(object s, int a)
{
//push a new modal page once coming back to current page
await Navigation.PushModalAsync(new ThirdPage());
}
private void Button_Clicked(object sender, EventArgs e)
{ // navigate to SecondPage
Navigation.PushModalAsync(new SecondPage());
}
}
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:c="clr-namespace:MauiApp0604"
BackgroundColor="Yellow"
Padding="20"
x:Class="MauiApp0604.MainPage">
<VerticalStackLayout
Padding="30,0"
Spacing="25">
<Button Text="To second" Clicked="Button_Clicked"/>
</VerticalStackLayout>
</ContentPage>
SecondPage.xaml.cs
public partial class SecondPage : ContentPage
{ //define mEventHander here
public static EventHandler<int> mEventHander;
public SecondPage()
{
InitializeComponent();
}
private async void Button_Clicked(object sender, EventArgs e)
{
await Navigation.PopModalAsync();
//use hander here
EventHandler<int> handler = mEventHander;
if (handler != null)
{
mEventHander(this,1);
}
}
}
SecondPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiApp0604.SecondPage"
Title="SecondPage">
<VerticalStackLayout>
<Label
Text="Welcome to second page"
VerticalOptions="Center"
HorizontalOptions="Center" />
<Button Text="To third" Clicked="Button_Clicked"></Button>
</VerticalStackLayout>
</ContentPage>