apache / apache/cordova-android
Keyboard focus not restored to last element on app resume
- Dominant language
- JavaScript
- Stars
- 3.8k
- Forks
- 1.6k
- Avg merge
- 16h 18m
- Merged PRs (30d)
- 11
Description
# Bug Report
## Problem
When resuming a cordova app, the keyboard focus is moved instead of being restored to the last focused element.
### What is expected to happen?
When the cordova app is paused and there was a focused element in the webview content, one expect the focus to be restored to that element when the app is resumed.
### What does actually happen?
The focus is moved to the first element in the page that has a non-negative tabIndex, otherwise to the document body (ignoring the elements with negative tabIndex).
## Information
This behavior has been introduced with this commit: https://github.com/apache/cordova-android/commit/c2cafb4b45fcbb307a113828dfc9f723f8a6433b
### Command or Code
- Create a new cordova app and add android platform.
- Add the following to `div.class` element in www/index.html:
```
```
- Add the following to www/css/index.css:
```
#focusable1:focus { background: yellow; }
#focusable2:focus { background: red; }
```
- Add the following to `app.receivedEvents` method in www/js/index.js:
```
document.getElementById('focusable1').focus();
```
- Compile and install the app
- On app launch, the first focusable element should have the focus, with yellow background
- Using mouse or keyboard, focus the second element, with red background
- Using adb, launch another app, e.g. `adb shell am start com.android.settings/.Settings`
- Resume cordova app, e.g. quit settings using the BACK button
Focus will be restored to the first element, with yellow background. If using `tabIndex="-1"` instead for the focusable elements, focus will be moved to the input element.
### Version information
cordova-android@7.0.0
## Checklist
- [X] I searched for existing GitHub issues
- [X] I updated all Cordova tooling to most recent version
- [X] I included all the necessary information above
Contributor guide
Research direction
Reproduce the resume flow using the steps in www/index.html, www/css/index.css, and www/js/index.js, including the app.receivedEvents method. Then read the linked commit that introduced the behavior and trace the app-resume focus handling. Done means the element focused before pause, including one with tabIndex="-1", receives focus again after resume rather than the first eligible element or document body.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- android, javascript
- Domain
- mobile
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100