Fix Android crash when setting Entry.Text in TextChanged event handler #32143
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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!
Description
Fixes a crash on Android when modifying
Entry.Text
within aTextChanged
event handler. The crash occurred with the error:Root Cause
The issue was a race condition in
EditTextExtensions.UpdateText()
whereSetSelection()
could be called with a position exceeding the actual text length. This happened because:TextChanged
event fires with the empty textText = "0"
to provide a default valueEmojiCompat
text watcher is still processing the deletion with the old text lengthSetSelection()
is called, the text length has changed, causing an out-of-bounds errorChanges
Added defensive bounds checking to all
SetSelection()
calls inEditTextExtensions.cs
:UpdateText(EditText, IEntry)
- Entry text updatesUpdateText(EditText, IEditor)
- Editor text updatesSetInputType()
- Cursor position restoration after input type changesThe fix uses
Math.Min()
to ensure the selection position never exceeds the current text length:Testing
Added UI test case that reproduces the original crash scenario:
Issues Fixed
Fixes android crash when setting Entry.Text in TextChanged event handler (related to EmojiCompat bounds checking)
Original prompt
This section details on the original issue you should resolve
<issue_title>end should be < than charSequence length</issue_title>
<issue_description>### Description
I am getting a crash when using
TextChanged
on aEntry
modifying the value.Steps to Reproduce
Link to public reproduction project repository
https://github.com/jdarwood007/MauiApp9
Version with bug
9.0.110 SR11
Is this a regression from previous behavior?
Not sure, did not test other versions
Last version that worked well
Unknown/Other
Affected platforms
Android
Affected platform versions
Android 14
Did you find any workaround?
The problem seems to be entirely calling this:
Relevant log output