Skip to content

Enable apps to control focus behavior during app lifecycle changes via FocusLifecyclePolicy - #190570

Merged
auto-submit[bot] merged 7 commits into
flutter:masterfrom
frank-weindel:feat/focus-lifecycle-policy
Nov 18, 2026
Merged

auto-submit[bot] merged 7 commits into
flutter:masterfrom
frank-weindel:feat/focus-lifecycle-policy

Conversation

@frank-weindel

@frank-weindel frank-weindel commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Closes #185311

This PR adds FocusLifecyclePolicy, an enum that controls FocusManager's behavior across app lifecycle transitions:

  • automatic (default) — matches existing platform-specific behavior: save/restore on desktop/web, preserve-as-is on Android/iOS.
  • suspendAndRestore — always save and clear focus on background, restore on foreground, regardless of platform.
  • preserve — never save/restore; focus state is left as-is across lifecycle transitions, regardless of platform.

Flutter developers who would like to control this behavior can set one of these enum values on the new FocusManager.instance.lifecyclePolicy property ideally once at app initialization. The behavior of the default automatic enum value matches the existing behavior prior to this PR so no breaking change is introduced by this PR.

See Also #142930 (Original implementation of App Lifecycle triggered focus behaviors)

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance.

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the gemini-code-assist bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

@google-cla

google-cla Bot commented Aug 4, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@github-actions github-actions Bot added framework flutter/packages/flutter repository. See also f: labels. f: focus Focus traversal, gaining or losing focus labels Aug 4, 2026
@frank-weindel
frank-weindel force-pushed the feat/focus-lifecycle-policy branch from 1942c9a to dd88157 Compare August 5, 2026 14:33
@frank-weindel

Copy link
Copy Markdown
Contributor Author

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

Fixed.

@frank-weindel
frank-weindel force-pushed the feat/focus-lifecycle-policy branch 2 times, most recently from 4cb5142 to 04cb4fe Compare August 5, 2026 19:28
@frank-weindel frank-weindel changed the title implement FocusLifecyclePolicy Enable developer control of FocusManager App Lifecycle behaviors via new FocusLifecyclePolicy options Aug 5, 2026
@frank-weindel frank-weindel changed the title Enable developer control of FocusManager App Lifecycle behaviors via new FocusLifecyclePolicy options Enable apps to control focus behavior during app lifecycle changes via FocusLifecyclePolicy Aug 5, 2026
@frank-weindel
frank-weindel force-pushed the feat/focus-lifecycle-policy branch 4 times, most recently from d7a77a6 to 1abbfb3 Compare August 5, 2026 23:06
@frank-weindel
frank-weindel marked this pull request as ready for review August 5, 2026 23:16

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces the FocusLifecyclePolicy enum to configure how FocusManager manages primary focus during application lifecycle changes, alongside corresponding updates and tests. The review comments correctly point out syntax errors where leading-dot shorthand was used for enum values in documentation, initialization, and switch expressions, which is unsupported in Dart.

@Piinks
Piinks self-requested a review August 11, 2026 22:25

@Piinks Piinks left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey @frank-weindel welcome! This looks pretty good. Couple notes below.
cc @chunhtai for 2nd review

@Piinks
Piinks requested a review from chunhtai August 17, 2026 21:00
@frank-weindel
frank-weindel force-pushed the feat/focus-lifecycle-policy branch from 4f44c49 to 3aeb4f9 Compare September 17, 2026 18:18
@frank-weindel

frank-weindel commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor Author

Updated based on feedback and also rebased. Sorry for the delay. I'll be more able to be more active with further feedback from this point on.

@chunhtai
chunhtai requested review from Piinks and chunhtai September 17, 2026 20:48
.suspendAndRestore => true,
.keepFocused => false,
};
if (shouldListen && _appLifecycleListener == null) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: can you add an assert(shouldListen == (_appLifecycleListener != null)) before the if condition so that it won't fail through unexpected situation?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Do you mean adding it after the if block? I think that makes the most sense here. If so, I've added it.

@frank-weindel
frank-weindel force-pushed the feat/focus-lifecycle-policy branch from 3aeb4f9 to fb92c13 Compare September 24, 2026 21:50
@frank-weindel

Copy link
Copy Markdown
Contributor Author

Rebased and addressed latest round of comments.

@chunhtai
chunhtai self-requested a review September 24, 2026 21:58
Piinks
Piinks previously approved these changes Oct 2, 2026

@Piinks Piinks left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM! I think this may need the formatter run on it to pass CI

@Piinks Piinks added the CICD Run CI/CD label Oct 2, 2026
chunhtai
chunhtai previously approved these changes Oct 2, 2026

@chunhtai chunhtai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@chunhtai chunhtai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@flutter-dashboard flutter-dashboard Bot removed the CICD Run CI/CD label Nov 16, 2026
@chunhtai chunhtai added CICD Run CI/CD autosubmit Merge PR when tree becomes green via auto submit App labels Nov 16, 2026
@auto-submit auto-submit Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Nov 16, 2026
@auto-submit

auto-submit Bot commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

autosubmit label was removed for flutter/flutter/190570, because - The status or check suite Presubmit has failed. Please fix the issues identified (or deflake) before re-applying this label.

@chunhtai chunhtai added the autosubmit Merge PR when tree becomes green via auto submit App label Nov 17, 2026
@auto-submit auto-submit Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Nov 17, 2026
@auto-submit

auto-submit Bot commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

auto label is removed for flutter/flutter/190570, Failed to enqueue flutter/flutter/190570 with HTTP 400: Pull request Required status check "Check Code Freeze" is expected..

@chunhtai chunhtai added the autosubmit Merge PR when tree becomes green via auto submit App label Nov 17, 2026
@auto-submit auto-submit Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Nov 17, 2026
@auto-submit

auto-submit Bot commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

auto label is removed for flutter/flutter/190570, Failed to enqueue flutter/flutter/190570 with HTTP 400: Pull request Required status check "Check Code Freeze" is expected..

@flutter-dashboard flutter-dashboard Bot removed the CICD Run CI/CD label Nov 17, 2026
@chunhtai chunhtai added autosubmit Merge PR when tree becomes green via auto submit App CICD Run CI/CD labels Nov 17, 2026
@auto-submit auto-submit Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Nov 17, 2026
@auto-submit

auto-submit Bot commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

autosubmit label was removed for flutter/flutter/190570, because - The status or check suite Presubmit has failed. Please fix the issues identified (or deflake) before re-applying this label.

@chunhtai chunhtai added the autosubmit Merge PR when tree becomes green via auto submit App label Nov 17, 2026
@auto-submit
auto-submit Bot added this pull request to the merge queue Nov 17, 2026
Merged via the queue into flutter:master with commit 7f49251 Nov 18, 2026
31 checks passed
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Nov 18, 2026
LouiseHsu pushed a commit to flutter/packages that referenced this pull request Nov 18, 2026
flutter/flutter@2f09f96...06f32f8

2026-11-18 [email protected] Move macOS
desktop builds to ThinLTO (flutter/flutter#196804)
2026-11-18 [email protected] Roll Dart SDK from
5ed55b4b2ebc to 753488ede427 (2 revisions) (flutter/flutter#196893)
2026-11-18 [email protected] Roll Packages from
9af69be to dca2e58 (7 revisions) (flutter/flutter#196892)
2026-11-18 [email protected] Roll Skia from 503a380ebecc
to 81ee7327e81c (29 revisions) (flutter/flutter#196889)
2026-11-18 [email protected] Roll Dart SDK from
26e38955d9d4 to 5ed55b4b2ebc (4 revisions) (flutter/flutter#196843)
2026-11-18 [email protected] Fix permissions for action updates
(flutter/flutter#196791)
2026-11-18 [email protected] iOS: Apply platform view mutators from
FlutterPlatformViewLayer (flutter/flutter#196753)
2026-11-18 [email protected] [flutter_tools] Remove ambient Doctor,
FeatureFlags, and device getters (flutter/flutter#196572)
2026-11-18 [email protected] Fix
RelativeRect.lerp crash when b is null (flutter/flutter#196655)
2026-11-18 [email protected] [web] Harden
WebP and GIF header readers against truncated and malformed buffers
(flutter/flutter#196075)
2026-11-17 [email protected] Enable apps to
control focus behavior during app lifecycle changes via
`FocusLifecyclePolicy` (flutter/flutter#190570)
2026-11-17 [email protected] Don't re-run the code freeze check
on label changes (flutter/flutter#196745)
2026-11-17 [email protected] Android: Report
FlutterView as a text editor while a text input client is attached
(flutter/flutter#193074)
2026-11-17 [email protected] Roll Packages from
4c6a7d3 to 9af69be (5 revisions) (flutter/flutter#196812)
2026-11-17 [email protected] [flutter_tools] Handle target VM filesystem
errors in DevFS.create (flutter/flutter#196234)
2026-11-17 [email protected] [flutter_tools] Clarify analytics reporting
status when on a feature branch (flutter/flutter#194496)
2026-11-17 [email protected] [web] Recover
CanvasKit surfaces from WebGL failures without per-frame null crashes
(flutter/flutter#196257)
2026-11-17 [email protected] [devicelab] Trigger navigation tap via
Flutter Driver in service_extensions_test (flutter/flutter#194456)

If this roll has caused a breakage, revert this CL and set the roller
to dry run mode using the controls here:
https://autoroll.skia.org/r/flutter-packages
Please CC [email protected],[email protected] on the revert to
ensure that a human
is aware of the problem.

To file a bug in Packages:
https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD f: focus Focus traversal, gaining or losing focus framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add FocusLifecyclePolicy to allow apps to control focus behavior during lifecycle changes

4 participants