Make android back button work for navigation properly

This commit is contained in:
2026-01-03 18:50:10 -06:00
parent b9a2a0127e
commit b65da4b9d5
2 changed files with 27 additions and 1 deletions

View File

@@ -1,7 +1,9 @@
using Android.App;
using System;
using Android.App;
using Android.Content.PM;
using Avalonia;
using Avalonia.Android;
using Avalonia.Controls.ApplicationLifetimes;
namespace CritterFolio.Android;
@@ -18,4 +20,22 @@ public class MainActivity : AvaloniaMainActivity<App>
return base.CustomizeAppBuilder(builder)
.WithInterFont();
}
public override void OnBackPressed()
{
if (Sys.Navigation != null && Sys.Navigation.IsLastPage())
{
if (Avalonia.Application.Current?.ApplicationLifetime is ISingleViewApplicationLifetime lifetime)
{
//base.OnBackPressed();
// CA1422: This call site is reachable on: 'Android' 21.0 and later. 'AvaloniaActivity.OnBackPressed()' is obsoleted on: 'Android' 33.0 and later.
// Just call exit from Sytem.Environment for now which does not shutdown cleanly
Environment.Exit(0);
}
}
else
{
Sys.Navigation?.PopPage();
}
}
}

View File

@@ -33,6 +33,11 @@ public partial class Navigation : UserControl
};
}
public bool IsLastPage()
{
return _pageStack.Count == 1;
}
public void Init(ContentControl pageContainer)
{
_pageContainer = pageContainer;
@@ -79,6 +84,7 @@ public partial class Navigation : UserControl
public void PopPage()
{
if (_pageStack.Count == 1) return;
Console.WriteLine($"Removing page {CurrentPage?.Title ?? "??"} from the page stack");
_pageStack.Pop();
RefreshPage();