Update predictive back docs - #13640
Conversation
There was a problem hiding this comment.
Code Review
This pull request updates the documentation for Android predictive back gesture support in Flutter, including instructions for FlutterFragment, PopScope migration, and related breaking changes. A typo was found in the breaking changes document where 'WithResult' was duplicated in a section header.
|
Staged preview of the updated docs.flutter.dev site (updated for commit fc35846): https://flutter-docs-prod--docs-pr13640-update-predictive-back-3ae2ylff.web.app |
|
Staged preview of the updated flutter.dev site (updated for commit fc35846): https://flutter-dev-230821--www-pr13640-update-predictive-back-tmpj9jhe.web.app |
…tive-back.md Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
justinmc
left a comment
There was a problem hiding this comment.
Just one clarification below, otherwise looks good.
| To support predictive back gestures in your Flutter application on | ||
| Android 13+: |
There was a problem hiding this comment.
Android 16 and above now sets enableOnBackInvokedCallback to true by default, so this step is only required on 13-15.
There was a problem hiding this comment.
Then these steps don't hurt for Android 16+ then either, right? It'd be nice to avoid making this conditional, since I guess many Android app developers want to support versions before 16 at this point anyway.
parlough
left a comment
There was a problem hiding this comment.
Thanks for tackling this @jesskuras!
Some suggestions and notes to consider. Feel free to take or leave them.
| The `PopScope` class directly replaces `WillPopScope` in order to enable | ||
| predictive back. Instead of deciding whether a pop is possible at the time it | ||
| occurs, this is set ahead of time with the `canPop` boolean. You can still | ||
| listen to pops by using `onPopInvoked`. |
There was a problem hiding this comment.
| listen to pops by using `onPopInvokedWithResult`. |
| used in a similar way to `onWillPop`. Keep in mind | ||
| that while `onWillPop` was called before the pop | ||
| was handled and had the ability to cancel it, | ||
| `onPopInvoked` is called after the pop is finished being handled. |
There was a problem hiding this comment.
| `onPopInvokedWithResult` is called after the pop is finished being handled. |
| ``` | ||
|
|
||
| For cases where it's necessary to be notified that a | ||
| pop was attempted, the `onPopInvoked` method can be |
There was a problem hiding this comment.
| pop was attempted, the `onPopInvokedWithResult` method can be |
| predictive back. If needed, use `PopScope` instead. | ||
| 1. Run the app and perform a back gesture (swipe from the | ||
| left side of the screen). | ||
| For complete setup instructions, guidelines on predictive back gesture |
There was a problem hiding this comment.
Love redirecting to the primary doc rather than trying to repeat this and keep it updated. Thanks!
| ) | ||
| ``` | ||
|
|
||
| ### Migrating from Form.onWillPop to Form.canPop and Form.onPopInvoked |
There was a problem hiding this comment.
| ### Migrating from Form.onWillPop to Form.canPop and Form.onPopInvokedWithResult |
| * `result`: An optional return payload passed when popping the route | ||
| (for example, via `Navigator.pop(context, result)`). |
There was a problem hiding this comment.
https://developers.google.com/style/word-list#via
| * `result`: An optional return payload passed when popping the route | |
| (for example, via `Navigator.pop(context, result)`). | |
| * `result`: An optional return payload passed when popping the route, | |
| for example, with `Navigator.pop(context, result)`. |
| ``` | ||
|
|
||
| [Android predictive back]: /release/breaking-changes/android-predictive-back | ||
| For more details on API migrations, check out the |
There was a problem hiding this comment.
Consider placing this in its own section as it seems separate from the proceeding example.
| For more details on API migrations, check out the | |
| ## More information | |
| For more details on API migrations, check out the |
| ## Handle back gestures with PopScope | ||
|
|
||
| To customize back navigation or prevent users from accidentally leaving a | ||
| screen, use the `PopScope` widget. `PopScope` replaces the deprecated |
There was a problem hiding this comment.
Consider linking the first use of PopScope to its API docs: https://api.flutter.dev/flutter/widgets/PopScope-class.html
| (for example, via `Navigator.pop(context, result)`). | ||
|
|
||
| * [Android predictive back][] breaking change | ||
| ### Example: Intercepting back navigation |
There was a problem hiding this comment.
I like the addition of an example!
| ```dart | ||
| return PopScope( | ||
| canPop: false, | ||
| onPopInvoked: (bool didPop) async { |
There was a problem hiding this comment.
| onPopInvokedWithResult: (bool didPop, Object? result) async { |
Fixes #13604
Summary
Updates documentation across the site to reflect the current state of Android predictive back support (Android 14+ default enablement) and recent API migrations:
platform-integration/android/predictive-back.md): Updated support status, documented Android manifest setup (enableOnBackInvokedCallback="true"), and added a usage example forPopScopewithonPopInvokedWithResult(bool didPop, Object? result).release/breaking-changes/android-predictive-back.md): Updated code snippets fromonPopInvokedtoonPopInvokedWithResultand refactored the setup section to delegate to the primary predictive back guide to avoid duplication.ui/navigation/index.md): Replaced deprecatedWillPopScopereferences withPopScope.add-to-app/android/add-flutter-fragment.md): Added documentation and Kotlin/Java examples for configuringshouldAutomaticallyHandleOnBackPressed(true).Staged pages
add-to-app/android/add-flutter-fragmentplatform-integration/android/predictive-backrelease/breaking-changes/android-predictive-backui/navigation