Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Conversation

@matthew-carroll
Copy link
Contributor

@matthew-carroll matthew-carroll commented Jun 26, 2019

Problem:
Flutter is not setup to handle backgrounding and then foregrounding an app when an input control has focus. The issue is visible in the new embedding because the new embedding loosens platform restrictions around FlutterActivity continuity.

Repro Steps:

  • Run an app with a TextField
  • Give focus to the TextField
  • Press Android's home button
  • Switch back to the app
  • Tap the TextField to give it focus again
  • An error appears in the logs.

The issue is that Flutter is unaware that the app was backgrounded and therefore expects the same input connection to exist when re-foregrounded. But on the Android side, the input connection was lost when the app was backgrounded and therefore needs to be recreated when foregrounded again.

Solution:
When FlutterActivity goes to the background and detaches from the FlutterEngine instance, instruct the framework to clear the input client. By clearing it, when the user taps on the control after being re-foregrounded, Flutter will request a new input connection and things should continue as expected.

Corresponding Framework PR:
flutter/flutter#35100

@xster
Copy link
Member

xster commented Jun 26, 2019

I assume this also fixes the case of a user opening a flutter activity, focuses on text, backs out of that flutter activity while keeping the same engine, then relaunches the same activity with the same engine and focuses on text again (assuming the same problem would exist there too)?

* The TextInputPlugin instance should not be used after calling this.
*/
public void destroy() {
if (inputTarget.type != InputTarget.Type.NO_TARGET) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should only do this when the target is a FRAMEWORK_CLIENT (if the target is a PLATFORM_VIEW _currentConnection on the framework side is probably null).

@matthew-carroll matthew-carroll force-pushed the 35054_text_input_error_when_home_and_back branch from a4fda95 to 53190bf Compare July 13, 2019 00:56
@matthew-carroll
Copy link
Contributor Author

I updated the PR with a different approach that utilizes InputConnection#closeConnection() instead of FlutterView destruction. This change in approach now solves the original but, as well as a bug where giving focus to a regular Android View did not result in Flutter UI from losing focus, or clearing of Flutter's input connection.

I also renamed the Android side message from 'clearClient' to 'onConnectionClosed'.

I'm still dubious about where and how I'm invoking unfocus(). If anyone can see a more canonical place to adjust focus, I'd like to hear it.

Copy link
Member

@xster xster left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Let's add tests

Copy link
Contributor

@mklim mklim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM modulo tests


textInputChannel.onConnectionClosed(mClient);

if (onConnectionClosed != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: This is final and never null, so I think it's okay to just always run this. Not a big deal and it doesn't hurt to be safe though.

related: annotations may be worthwhile in the constructor?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I'll add annotations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parameters are now annotated and the if-statement has been removed.

@matthew-carroll
Copy link
Contributor Author

Rebasing to resolve conflicts.

@matthew-carroll matthew-carroll force-pushed the 35054_text_input_error_when_home_and_back branch from 818f8dc to 29155ee Compare July 22, 2019 20:33
@cbracken
Copy link
Member

cbracken commented Aug 5, 2019

@matthew-carroll is this mergeable?

@matthew-carroll
Copy link
Contributor Author

I have to figure out why the tests are failing and fix them.

@matthew-carroll matthew-carroll force-pushed the 35054_text_input_error_when_home_and_back branch 2 times, most recently from 45b0f62 to 52041d0 Compare August 16, 2019 21:43
@matthew-carroll
Copy link
Contributor Author

@xster @mklim I added a unit test

Copy link
Contributor

@mklim mklim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a lot of files in a testing/embedding/android/focus_new_activity_back/ that look like they've been generated as part of a tool/IDE and shouldn't be committed to source, including binary files and apk build files. I think we probably want to apply flutter/flutter's gitignore here.

New unit test LG.

@@ -0,0 +1 @@
android_intent=/Users/mattcarroll/.pub-cache/hosted/pub.dartlang.org/android_intent-0.3.1+1/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty sure this should be reverted. We probably should add files with this extension to the .gitignore.

@xster
Copy link
Member

xster commented Aug 17, 2019

+1. It's a bit hard to review since all the build intermediaries are checked in.

@matthew-carroll matthew-carroll force-pushed the 35054_text_input_error_when_home_and_back branch from fb54ca2 to 4e81b70 Compare August 17, 2019 06:52
@xster
Copy link
Member

xster commented Aug 17, 2019

LGTM

engine-flutter-autoroll added a commit to flutter/flutter that referenced this pull request Aug 20, 2019
[email protected]:flutter/engine.git/compare/21ae92651a38...3e10d92

git log 21ae926..3e10d92 --no-merges --oneline
2019-08-19 [email protected] Roll src/third_party/dart 0e201edeeb..9f13d07670 (27 commits)
2019-08-19 [email protected] Notify framework to clear input connection when app is backgrounded (#35054) (flutter/engine#9498)
2019-08-19 [email protected] Re-enable firebase test and don't use google login (flutter/engine#11228)
2019-08-19 [email protected] Update tflite_native and language_model revisions to match the Dart SDK (flutter/engine#11230)
2019-08-19 [email protected] Roll src/third_party/skia f8221786d088..8566dda51b42 (4 commits) (flutter/engine#11220)
2019-08-19 [email protected] Update metal layer drawable size on relayout. (flutter/engine#11224)
2019-08-19 [email protected] Make firebase testlab always pass (flutter/engine#11226)

The AutoRoll server is located here: https://autoroll.skia.org/r/flutter-engine-flutter-autoroll

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+/master/autoroll/README.md

If the roll is causing failures, please contact the current sheriff ([email protected]), and stop
the roller if necessary.
matthew-carroll added a commit to matthew-carroll/engine that referenced this pull request Aug 29, 2019
matthew-carroll added a commit to matthew-carroll/engine that referenced this pull request Aug 29, 2019
matthew-carroll added a commit that referenced this pull request Aug 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants