-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Fix backspace behavior after cut operation by resetting selection to cursor position. #19500
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
base: main
Are you sure you want to change the base?
Conversation
Fix backspace behavior after cut operation by resetting selection to cursor position.
|
What causes this bug to occur in the first place? This doesn't occur on regular I suspect we can fix this in a more simple manner once the code which causes this issue is identified. |
|
I stand corrected, I can replicate this on the |
|
The issue likely persists because pastePlainText() still uses setText(), which disrupts Android’s Editor state and leads to stale selection during cut operations. Switching to Editable.replace() / insert() methods for pasting should better preserve the Editor’s internal state and help resolve the problem. |
|
Fixed root cause in FieldEditText by changing pastePlainText() to use Editable.replace() instead of setText(), preserving editor state and fixing cut/backspace issues. Create Deck dialog uses standard TextInputEditText, so issue there may differ. Should I check Create Deck dialog too or is this fix enough? |
Replaced setText() with Editable.replace() to maintain Editor state during paste operations.
david-allison
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.
Can you confirm that you tested this? The fix doesn't work for me
AnkiDroid Version = 2.23.0alpha8-debug (ffcd536cc23c820a0d5354403c7335f35bff0418)
Screen_recording_20251116_230548.webm
|
Screen_recording_20251117_050427.webm |
|
The issue only occurs if:
|
|
Despite having my mathematics-3, i spent fixing this and was much fun. :) |
|
Much appreciated, but school takes priority, always! (we'll be here when you have free time, and my schooling significantly changed the direction of my life) Running on an API 36 Tablet emulator
Screen_recording_20251117_020230.webm |

Fix backspace behavior after cut operation by resetting selection to cursor position.
Purpose / Description
After cutting text with CTRL+X in the note editor, pressing backspace would remove multiple characters (equal to the number of characters that were cut) instead of just one character. This bug occurred when using a physical keyboard with AnkiDroid.
Steps to reproduce the bug:
Fixes
Approach
The issue was caused by the selection range not being properly reset after the cut operation. When text was cut using CTRL+X, Android's default cut behavior would remove the selected text, but the selection state (start and end positions) might still retain the old range. When backspace was pressed afterward, it would use this stale selection range instead of just removing a single character at the cursor position.
Solution:
FieldEditText.onTextContextMenuItem()How Has This Been Tested?
Manual Testing:
Test Configuration:
Learning (optional, can help others)
Research:
EditTextbehavior with keyboard shortcutsonTextContextMenuItem()is called for both context menu actions and keyboard shortcuts (CTRL+X, CTRL+V, etc.)Key Insight:
Android's default cut implementation in
EditTexthandles the clipboard and text removal, but doesn't always properly reset the selection state. By explicitly managing the selection after cut operations, we ensure consistent behavior.Checklist