-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expose Android Activity and Modal Dialog OnKey* overrides via ConfigureLifecycleEvents with generalized context parameter #31206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
@copilot, make sure you also apply these to the dialog in modalnavigationmanager.android.cs |
Added OnKey* lifecycle event overrides to the CustomComponentDialog class in the modal navigation manager. The implementation follows the same pattern as the main activity and includes proper null checking for KeyEvent parameters. All OnKey* events (OnKeyDown, OnKeyUp, OnKeyLongPress, OnKeyMultiple, OnKeyShortcut) are now supported in modal dialogs as well. Changes committed in e6fdede. |
|
/azp run |
|
Azure Pipelines successfully started running 3 pipeline(s). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR exposes Android Activity and Modal Dialog OnKey* overrides via ConfigureLifecycleEvents, allowing .NET MAUI developers to handle hardware key events (volume buttons, back button, keyboard shortcuts, etc.) directly through lifecycle events. This provides cross-platform access to Android-specific key handling without requiring custom activity subclassing.
Key changes:
- Adds five new lifecycle event delegates (OnKeyDown, OnKeyUp, OnKeyLongPress, OnKeyMultiple, OnKeyShortcut) to AndroidLifecycle
- Implements override methods in MauiAppCompatActivity and modal dialog classes to invoke these lifecycle events
- Provides comprehensive unit tests and sample implementation demonstrating the functionality
Reviewed Changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
src/Core/src/LifecycleEvents/Android/AndroidLifecycle.cs |
Adds five new delegate definitions for OnKey* events with appropriate signatures |
src/Core/src/LifecycleEvents/Android/AndroidLifecycleBuilderExtensions.cs |
Adds extension methods to register OnKey* event handlers via builder pattern |
src/Core/src/Platform/Android/MauiAppCompatActivity.Lifecycle.cs |
Implements OnKey* method overrides that invoke lifecycle events and handle return values |
src/Controls/src/Core/Platform/ModalNavigationManager/ModalNavigationManager.Android.cs |
Implements OnKey* overrides for modal dialogs with proper activity context handling |
src/Core/tests/UnitTests/LifecycleEvents/LifecycleEventsTests.cs |
Adds comprehensive unit tests covering all OnKey* events and multiple handler scenarios |
src/Core/src/PublicAPI/net-android/PublicAPI.Unshipped.txt |
Updates public API surface with new lifecycle event delegates and extension methods |
src/Controls/samples/Controls.Sample.Sandbox/MauiProgram.cs |
Demonstrates usage of OnKey* events with volume and back button handling |
src/Controls/samples/Controls.Sample.Sandbox/MainPage.xaml |
Provides user-facing documentation and examples of the new functionality |
You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.
src/Controls/src/Core/Platform/ModalNavigationManager/ModalNavigationManager.Android.cs
Outdated
Show resolved
Hide resolved
src/Controls/src/Core/Platform/ModalNavigationManager/ModalNavigationManager.Android.cs
Outdated
Show resolved
Hide resolved
PureWeen
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot revert changes made to the sandbox project and please update your instructions so that you know to never make changes to the sandbox project
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot revert these changes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reverted changes to MainPage.xaml. The file is now back to its original state. Changes committed in 1a8c24f.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot revert these changes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reverted changes to MauiProgram.cs. The file is now back to its original state. Changes committed in 1a8c24f.
Reverted all changes to the sandbox project files (MainPage.xaml and MauiProgram.cs) as requested. I've updated my internal process to avoid making changes to sandbox projects in the future. Changes committed in 1a8c24f. |
| this.OnBackPressedDispatcher.AddCallback(new CallBack(true, this)); | ||
| } | ||
|
|
||
| public override bool OnKeyDown(Keycode keyCode, KeyEvent? e) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot I don't think the second argument on these needs to be nullable it looks like these override signatures will just be KeyEvent e vs KeyEvent? e
Also, can we make all of this implementation more generalized?
We don't need to pass the activity into the InvokeLifecycleEvents call.
Just make it so the InvokeLifecycleEvents takes a type object and then we can just pass the dialog directly in for when it's the dialog calling it and pass the activity in when it's the activity calling it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed both issues:
-
KeyEvent nullability: Changed dialog override signatures from
KeyEvent? etoKeyEvent eto match Android Dialog API (Activity methods correctly remain nullable) -
Generalized implementation:
- Updated
AndroidLifecycle.OnKey*delegates to useobject contextinstead ofActivity activity - Dialog implementation now passes
thisdirectly and removes activity extraction logic - Activity implementation already correctly passed
this
- Updated
Changes implemented in 060c6d8. All existing unit tests updated and passing.
Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
…entation Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
060c6d8 to
da3cbca
Compare
|
/azp run |
|
Azure Pipelines successfully started running 3 pipeline(s). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot revert changes to this file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reverted changes to LifecycleEventsTests.cs as requested. The file is now back to its original state with the test parameter names using activity instead of context. Changes committed in 6c4519b.
Co-authored-by: PureWeen <5375137+PureWeen@users.noreply.github.com>
|
/azp run |
|
Azure Pipelines successfully started running 3 pipeline(s). |
|
/backport to release/10.0.1xx-rc1 |
|
Started backporting to release/10.0.1xx-rc1: https://github.com/dotnet/maui/actions/runs/17073544649 |
Note
Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!
This PR adds new lifecycle event delegates to ConfigureLifecycleEvents for Android that expose all of the OnKey* methods available on Android Activities and Modal Dialogs. This allows developers using .NET MAUI to handle key events (such as OnKeyDown, OnKeyUp, OnKeyLongPress, etc.) directly in their MAUI applications via lifecycle events, providing finer control over hardware key interactions in both main activities and modal dialogs.
Public API Changes
The following new lifecycle event delegates have been added to
AndroidLifecycle:OnKeyDown- Fires when a hardware key is pressed downOnKeyUp- Fires when a hardware key is releasedOnKeyLongPress- Fires when a hardware key is held down for an extended periodOnKeyMultiple- Fires for repeated key eventsOnKeyShortcut- Fires for keyboard shortcutsThese delegates use a generalized
object contextparameter instead ofActivity activity, allowing them to work with both Activities and Modal Dialogs.Usage Example
Implementation Details
AndroidLifecycle.cswithobject contextparameter for flexibilityAndroidLifecycleBuilderExtensions.csMauiAppCompatActivity.Lifecycle.csModalNavigationManager.Android.csfor modal dialogsKeyEvent eto match Android Dialog API, while Activity overrides correctly use nullableKeyEvent? eboolto indicate whether the event was handledTesting
This feature enables apps to react to hardware key events (volume buttons, keyboard, etc.) in both main activities and modal dialogs in a cross-platform way. The generalized context parameter makes the implementation more flexible and maintainable. For example, games, accessibility tools, or apps requiring custom key handling can implement platform-specific logic without needing to subclass native Android activities or handlers.
Fixes #31205.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.