nowinandroid
nowinandroid copied to clipboard
Retain focus and keyboard visibility of SearchTextField after configuration changes
What I have done and why Currently, the app opens keyboard after configuration changed even when a user hid the keyboard by tapping back button or 'search' key. Also, position of cursor is not retained after the event. Therefore, I...
- added
focusRestorer
modifier to retain focus and keyboard keyboard - added logic to retain focus and keyboard after configuration changed
- added logic to not view keyboard when a user canceled keyboard by tapping back button or 'search' button
- added logic to ignore change of focus and keyboard during configuration changes
Fixes #1033
Considerations
- Need to cache focus flag value during initialization to avoid incorrect modification during initial composition
- Need to ignore focus changes while the activity is changing configurations
- Need to work well with multiple
TextField
s
Do tests pass?
- [x] Run local tests on
DemoDebug
variant:./gradlew testDemoDebug
- [x] Check formatting:
./gradlew --init-script gradle/init.gradle.kts spotlessApply
Is this your first pull request?
- [x] Sign the CLA
- [x] Run
./tools/setup.sh
- [x] Import the code formatting style as explained in the setup script.
As per advice from @JolandaVerhoef, before merging this PR, I will wait for Compose Engineers' opinion on this workaround. I will share them code on internal shadow for this issue: https://issuetracker.google.com/192776771
I created another repository to highlight this workaround.
Repo: https://github.com/saryongkang/focus-restorable-textfield Screen recording: https://github.com/android/nowinandroid/assets/978709/93beaef7-ba2f-4f35-a6b3-d90ccfc094b7
There are a few issues:
- The cursor position is not retained after a configuration change
- IME state is not retained after configuration changes in all cases. Try changing configuration 5 times in a row, and notice the inconsistency. Sometimes IME state would be preserved and sometimes not.
Tested on a physical device, Pixel 6a, Android 13