Replies: 4 comments 13 replies
-
We currently do not have a control type that represents an Activity, like what with have for desktop platforms with |
Beta Was this translation helpful? Give feedback.
-
As I understand it, Avalonia's support for Android apps is to model them as a Single Page Application (SPA). That is, there will only ever be one activity, and any Back button behavior will have to be emulated within your application.
I found a way to do this. The tricky part is that the Android back button is not plumbed through Avalonia at all, nor is the Android view or view model plumbed through to the Activity. I had to rely on a static field (which already existed, FWIW) so that the Activity could communicate with my view model in order to emulate the expected Back button behavior. Here is how I did it:
public override void OnBackPressed()
{
if (App.Current?.MainViewModel is { } mainViewModel && mainViewModel.NavigateBackCommand.CanExecute(null))
{
mainViewModel.NavigateBackCommand.Execute(null);
}
else
{
// This will terminate the app.
base.OnBackPressed();
}
} My To provide an equivalent experience in desktop applications, which have no OS-provided Back button, my MainWindow adds a Back button in addition to the MainView user control that takes up most of the window. |
Beta Was this translation helpful? Give feedback.
-
To handle navigation, there's the option of using MvvmDialogs which now supports navigation for mobile. I just released preview2 with mobile support, although doc isn't updated yet. BackRequested is now supported by Avalonia? That will make things much simpler for MvvmDialogs! I'll be able to add that quickly then. |
Beta Was this translation helpful? Give feedback.
-
There is still nothing about this in the documentation. After thinking about it, I came to this approach: // Android.MainActivity.cs
public override void OnBackPressed()
{
var navigatorService = App.GetService<INavigatorService>();
if (navigatorService == null)
{
return;
}
if (navigatorService.IsHome())
{
// if In Home Page - exit Application
OnBackPressedDispatcher.OnBackPressed();
}
else
{
navigatorService.OpenPrevious();
}
} Here I use Microsof Dependency Injection and my custom View Navigator service. This approach is push code from MainView.cs to Android MainActivity.cs |
Beta Was this translation helpful? Give feedback.
-
Does Avalonia's on Android include full support for activities and navigation? I wasn't able to find any documentation on how you would set this up. Here are some of the questions I had:
Beta Was this translation helpful? Give feedback.
All reactions