Add support for custom handling of Android back button in Blazor Hybrid apps
- Dominant language
- C#
- Stars
- 23.3k
- Forks
- 2k
- Avg merge
- 1d 15h
- Merged PRs (30d)
- 290
Description
### Description
I'd like to be able to inject a service that I can subscribe to Android back button events, and prevent the default behaviour of the Android back button when necessary. This is currently not possible within Blazor Hybrid apps.
### Public API Changes
Ideally, it would be nice to have a service that could be injected and subscribed to, similar to the NavigationManager. with the ability to override the default behaviour if necessary,
```csharp
///
/// Represents a method that handles an android back button.
///
/// The source of the event
/// The event data
/// Whether or not the back navigation was handled by the override.
///
/// If you want to handle or cancel the navigation yourself, you can do so in this method and then return true.
///
/// Note that this only works on Android for the hardware back-button. On iOS, this event will never be called
/// because there is no hardware back-button.
///
public delegate bool BackButtonEventHandler(object? sender, EventArgs args);
///
/// Manager for the Back Button in Android
///
public interface IBackButtonManager
{
event BackButtonEventHandler OnBackButtonPressed;
}
```
## Usage Examples
Code behind for a Popup.razor component
```csharp
public partial class Popup : IDisposable
{
protected override void OnInitialized()
{
backButtonManager.OnBackButtonPressed += BackButtonManager_OnBackButtonPressed;
}
private bool BackButtonManager_OnBackButtonPressed(object sender, EventArgs args)
{
ClosePopup();
return true;
}
private void ClosePopup()
{
// close popup code here...
}
public void Dispose()
{
backButtonManager.OnBackButtonPressed -= BackButtonManager_OnBackButtonPressed;
}
}
```
## Alternative Designs
Alternatively the `Page.OnBackButtonPressed` method could be utilised, and similar methods of creating services on an app-by-app basis could be used. However, currently the OnBackButtonPressed method is never hit 😢
## Risks
It does introduce some ambiguity if the event is subscribed to multiple times.
### Intended Use-Case
Currently the Android back button will navigate Blazor app back to the previous page, however, this is not always desirable. In some cases I'd like to manually handle the android back button, such as for:
1. Closing a modal or popup window, or
2. Preventing the user from navigating back after authenticating, and instead closing the app.
There is the `Page.OnBackButtonPressed` method which can be overriden, however, it does not get called from within a Blazor Hybrid app.
```csharp
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
protected override bool OnBackButtonPressed()
{
// This line will never get hit :(
return false;
}
}
```
Contributor guide
Research direction
Begin at the MainPage.OnBackButtonPressed example and trace how Android back navigation reaches a Blazor Hybrid app. Define and verify a supported interception path that lets an app handle or cancel the event without default navigation; the issue names no source files or tests, so locate those entry points first.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- android, csharp
- Domain
- mobile-dev
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100