OnBackPressedCallback support for Android 16+#22289
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds support for Android 15+'s new OnBackPressedCallback API to handle back navigation with predictive back gesture support. The key change is that on Android 15+, subscription state to the BackRequested event determines whether the app handles back navigation, rather than the Handled property.
Key Changes
- Modified
SystemNavigationManagerto track subscription state and notify Android-specific code when subscribers are added/removed - Added Android 15+ specific back handling using
OnBackPressedCallbackthat integrates with the new predictive back gesture system - Enhanced the sample app with comprehensive testing UI for both Android 15+ and pre-Android 15 behaviors
- Added detailed documentation explaining the behavioral differences and best practices
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
src/Uno.UWP/UI/Core/SystemNavigationManager.cs |
Converted BackRequested event to property syntax with add/remove accessors to track subscription state; added internal property and partial method for Android integration |
src/Uno.UWP/UI/Core/SystemNavigationManager.Android.cs |
New Android-specific partial implementation that raises static event when subscription state changes |
src/Uno.UI/UI/Xaml/ApplicationActivity.BackPressed.Android.cs |
New partial class file implementing OnBackPressedCallback initialization/cleanup and Android 15+ back handling logic |
src/Uno.UI/UI/Xaml/ApplicationActivity.Android.cs |
Integrated callback initialization in OnCreate and cleanup in OnDestroy |
src/Uno.UI.Runtime.Skia.Android/Uno.UI.Runtime.Skia.Android.csproj |
Added link to shared BackPressed.Android.cs file for Skia Android support |
src/Uno.UI.Runtime.Skia.Android/ApplicationActivity.cs |
Integrated callback initialization in OnCreate and cleanup in OnDestroy for Skia Android |
src/SamplesApp/UITests.Shared/Windows_UI_Core/SystemNavigationManagerTests/HardwareBackButton.xaml |
Comprehensive test UI with platform detection, subscription controls, and detailed logging |
src/SamplesApp/UITests.Shared/Windows_UI_Core/SystemNavigationManagerTests/HardwareBackButton.xaml.cs |
Test implementation with Android version detection, subscription management, and platform-specific behavior demonstration |
doc/articles/features/hardware-back-button.md |
Added documentation for Android 15+ behavior changes and best practices for subscription-based back handling |
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-22289/wasm-skia-net9/index.html |
|
The build 190216 found UI Test snapshots differences: Details
|
|
🤖 Your Docs stage site is ready! Visit it here: https://unodocsprstaging.z13.web.core.windows.net/pr-22289/docs/index.html |
|
The build 190218 found UI Test snapshots differences: Details
|
morning4coffe-dev
left a comment
There was a problem hiding this comment.
The sub to BackRequested now implicitly marks the event as handled on A16+, regardless of whether the user sets args.Handled = true, do we/should we count with the possibility of user setting it to false? I think it would make the behavior closer to... well UWP 😅.
OnBackPressedCallback support for Android 15+OnBackPressedCallback support for Android 16+
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 11 out of 11 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (1)
src/Uno.UWP/UI/Core/SystemNavigationManager.cs:20
- The
_backIsAlwaysHandledfield is set every timeGetForCurrentView()is called, but this is a singleton instance. This means the field can be set multiple times unnecessarily, and its value could change if the Android version check behavior changes during runtime (which shouldn't happen, but the pattern is inconsistent). Consider initializing this field once in the constructor or as a field initializer, since the Android SDK version cannot change during the lifetime of the application.
}
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-22289/wasm-skia-net9/index.html |
|
The build 190608 found UI Test snapshots differences: Details
|
|
🤖 Your Docs stage site is ready! Visit it here: https://unodocsprstaging.z13.web.core.windows.net/pr-22289/docs/index.html |
|
The build 190610 found UI Test snapshots differences: Details
|
|
The build 190606 found UI Test snapshots differences: Details
|
|
The build 190612 found UI Test snapshots differences: Details
|
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-22289/wasm-skia-net9/index.html |
|
🤖 Your Docs stage site is ready! Visit it here: https://unodocsprstaging.z13.web.core.windows.net/pr-22289/docs/index.html |
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-22289/wasm-skia-net9/index.html |
|
The build 190638 found UI Test snapshots differences: Details
|
|
🤖 Your Docs stage site is ready! Visit it here: https://unodocsprstaging.z13.web.core.windows.net/pr-22289/docs/index.html |
|
The build 190677 found UI Test snapshots differences: Details
|
|
The build 190677 found UI Test snapshots differences: Details
|
|
|
|
The build 190677 found UI Test snapshots differences: Details
|
|
The build 190677 found UI Test snapshots differences: Details
|
GitHub Issue: closes #11748
PR Type: ✨ Feature
What is the current behavior? 🤔
On Android 16+
OnBackPressedis no longer called. InsteadOnBackPressedCallbackneeds to be used, but it has different semantics.What is the new behavior? 🚀
OnBackPressedCallbacksupportedPR Checklist ✅
Please check if your PR fulfills the following requirements:
Screenshots Compare Test Runresults.