[Android] Add predictive back support for FlutterFragment and FlutterFragmentActivity#181124
Conversation
There was a problem hiding this comment.
Code Review
This pull request adds support for predictive back gestures in FlutterFragment and FlutterFragmentActivity on Android 14 (API 34) and higher. The implementation correctly uses an SDK version check to provide different OnBackPressedCallback implementations. For API 34+, it handles the new back gesture events (start, progress, commit, cancel) and delegates them to FlutterActivityAndFragmentDelegate. For older versions, it maintains the existing onBackPressed behavior. The changes are accompanied by new Robolectric tests that verify the behavior on both SDK 33 and SDK 34, ensuring the new logic is correctly triggered. The code is well-structured and the changes are solid. I have one minor suggestion to improve test code maintainability.
| verify(mockDelegate, times(1)).startBackGesture(any()); | ||
| verify(mockDelegate, times(1)).updateBackGestureProgress(any()); | ||
| verify(mockDelegate, times(1)).commitBackGesture(); | ||
| verify(mockDelegate, times(1)).cancelBackGesture(); |
There was a problem hiding this comment.
This block of verification logic is very similar to the one on lines 390-393. To improve readability and reduce code duplication, consider extracting this logic into a private helper method.
For example, you could create a method like this:
private void verifyBackGestureCalls(FlutterActivityAndFragmentDelegate mockDelegate, org.mockito.verification.VerificationMode mode) {
verify(mockDelegate, mode).startBackGesture(any());
verify(mockDelegate, mode).updateBackGestureProgress(any());
verify(mockDelegate, mode).commitBackGesture();
verify(mockDelegate, mode).cancelBackGesture();
}Then you could call it with times(0) and times(1) respectively.
|
FlutterFragmentActivity: Screen_recording_20260119_005512.webmFlutterFragment with another Fragment: Screen_recording_20260119_005806.webmCode that I used to check behaviormain.dart: import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(primarySwatch: Colors.blue),
home: const MainScreen(),
);
}
}
class MainScreen extends StatelessWidget {
const MainScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Main")),
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => DetailScreen()),
);
},
child: Text('See details'),
),
),
);
}
}
class DetailScreen extends StatelessWidget {
const DetailScreen({super.key});
@override
Widget build(BuildContext context) {
final message = "Details";
return Scaffold(
appBar: AppBar(title: Text(message)),
body: Center(child: Text(message)),
);
}
}MainActivity.kt: class MainActivity : FlutterFragmentActivity()class MainActivity : FragmentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
supportFragmentManager.beginTransaction()
.add(android.R.id.content, InitialFragment())
.commit()
}
class InitialFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
@SuppressLint("SetTextI18n")
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val button = Button(activity)
button.text = "Go Flutter"
button.setOnClickListener {
val activity = activity ?: return@setOnClickListener
val flutterFragment = FlutterFragment.NewEngineFragmentBuilder()
.shouldAutomaticallyHandleOnBackPressed(true)
.build<FlutterFragment>()
activity.supportFragmentManager.beginTransaction()
.add(android.R.id.content, flutterFragment)
.addToBackStack("Flutter")
.commit()
}
return button
}
}
} |
|
autosubmit label was removed for flutter/flutter/181124, because The base commit of the PR is older than 7 days and can not be merged. Please merge the latest changes from the main into this branch and resubmit the PR. |
|
@moko256 thank you for your contribution. I am investigating the internal failure and should have an answer for you today. Right now I believe there is nothing you need to do on your side. |
|
Google3 testing failure is overridden by me. Thank you again for your contribution! |
…nd FlutterFragmentActivity (flutter/flutter#181124)
…nd FlutterFragmentActivity (flutter/flutter#181124)
flutter/flutter@9eafba4...c305f1f 2026-02-02 146823759+brahim-guaali@users.noreply.github.com Fix DecoratedSliver sample to avoid antialiasing gap (flutter/flutter#179848) 2026-02-02 koutaro.mo@gmail.com [Android] Add predictive back support for FlutterFragment and FlutterFragmentActivity (flutter/flutter#181124) 2026-02-02 engine-flutter-autoroll@skia.org Roll Packages from 510dd40 to 837dbbd (3 revisions) (flutter/flutter#181811) 2026-02-02 engine-flutter-autoroll@skia.org Roll Dart SDK from 434c5c6d1889 to 1aa8f2de7587 (1 revision) (flutter/flutter#181810) 2026-02-02 146823759+brahim-guaali@users.noreply.github.com Use null-aware elements in cupertino/list_tile.dart (flutter/flutter#181243) 2026-02-02 146823759+brahim-guaali@users.noreply.github.com Use null-aware elements in material/dialog.dart (flutter/flutter#181244) 2026-02-02 146823759+brahim-guaali@users.noreply.github.com Use null-aware spread in cupertino/app.dart (flutter/flutter#181585) 2026-02-02 146823759+brahim-guaali@users.noreply.github.com Use null-aware spread in material/app.dart (flutter/flutter#181586) 2026-02-02 engine-flutter-autoroll@skia.org Roll Dart SDK from 84abc8d58ec8 to 434c5c6d1889 (1 revision) (flutter/flutter#181800) 2026-02-02 engine-flutter-autoroll@skia.org Roll Skia from c14c5b243395 to 43fa79e1c51f (8 revisions) (flutter/flutter#181797) 2026-02-02 engine-flutter-autoroll@skia.org Roll Skia from 4ee9eb659ae0 to c14c5b243395 (1 revision) (flutter/flutter#181780) 2026-02-02 engine-flutter-autoroll@skia.org Roll Fuchsia Linux SDK from nI52U4LJMrBv8G1M9... to 1L4m9qCikk-JzrNWE... (flutter/flutter#181779) If this roll has caused a breakage, revert this CL and stop the roller using the controls here: https://autoroll.skia.org/r/flutter-packages Please CC stuartmorgan@google.com 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
…FragmentActivity (flutter#181124) <!-- Thanks for filing a pull request! Reviewers are typically assigned within a week of filing a request. To learn more about code review, see our documentation on Tree Hygiene: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md --> This PR will wire FlutterFragment up to BackGestureChannel via FlutterActivityAndFragmentDelegate. When shouldAutomaticallyHandleOnBackPressed is set to true, FlutterFragment will handle back gesture events. Since FlutterFragmentActivity uses FlutterFragment with this property enabled, it will also handle these events as a result of this PR. Fixes: flutter#149753 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). 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. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
…FragmentActivity (flutter#181124) <!-- Thanks for filing a pull request! Reviewers are typically assigned within a week of filing a request. To learn more about code review, see our documentation on Tree Hygiene: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md --> This PR will wire FlutterFragment up to BackGestureChannel via FlutterActivityAndFragmentDelegate. When shouldAutomaticallyHandleOnBackPressed is set to true, FlutterFragment will handle back gesture events. Since FlutterFragmentActivity uses FlutterFragment with this property enabled, it will also handle these events as a result of this PR. Fixes: flutter#149753 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). 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. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
This PR will wire FlutterFragment up to BackGestureChannel via FlutterActivityAndFragmentDelegate.
When shouldAutomaticallyHandleOnBackPressed is set to true, FlutterFragment will handle back gesture events.
Since FlutterFragmentActivity uses FlutterFragment with this property enabled, it will also handle these events as a result of this PR.
Fixes: #149753
Pre-launch Checklist
///).If you need help, consider asking for advice on the #hackers-new channel on Discord.
Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the
gemini-code-assistbot 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.