Skip to content

[Android] Add predictive back support for FlutterFragment and FlutterFragmentActivity#181124

Merged
auto-submit[bot] merged 3 commits intoflutter:masterfrom
moko256:149753-flutter-fragment-predictive-back
Feb 2, 2026
Merged

[Android] Add predictive back support for FlutterFragment and FlutterFragmentActivity#181124
auto-submit[bot] merged 3 commits intoflutter:masterfrom
moko256:149753-flutter-fragment-predictive-back

Conversation

@moko256
Copy link
Contributor

@moko256 moko256 commented Jan 18, 2026

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-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.

@moko256 moko256 requested a review from a team as a code owner January 18, 2026 15:38
@github-actions github-actions bot added platform-android Android applications specifically engine flutter/engine related. See also e: labels. team-android Owned by Android platform team labels Jan 18, 2026
Copy link
Contributor

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

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 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.

Comment on lines +402 to +405
verify(mockDelegate, times(1)).startBackGesture(any());
verify(mockDelegate, times(1)).updateBackGestureProgress(any());
verify(mockDelegate, times(1)).commitBackGesture();
verify(mockDelegate, times(1)).cancelBackGesture();
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

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.

@moko256
Copy link
Contributor Author

moko256 commented Jan 18, 2026

FlutterFragmentActivity:

Screen_recording_20260119_005512.webm

FlutterFragment with another Fragment:

Screen_recording_20260119_005806.webm
Code that I used to check behavior

main.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
        }
    }
}

@camsim99 camsim99 requested review from gmackall and justinmc January 20, 2026 21:54
Copy link
Contributor

@justinmc justinmc left a comment

Choose a reason for hiding this comment

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

@moko256 Thank you so much for contributing this! I've been hoping we'd add support here for a long time but we never got the time to do it.

I'll defer to @gmackall for final approval but this looks good to me 👍

Copy link
Member

@gmackall gmackall left a comment

Choose a reason for hiding this comment

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

Thanks!

@gmackall gmackall added the autosubmit Merge PR when tree becomes green via auto submit App label Jan 27, 2026
@auto-submit auto-submit bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Jan 27, 2026
@auto-submit
Copy link
Contributor

auto-submit bot commented Jan 27, 2026

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
Copy link
Contributor Author

moko256 commented Feb 2, 2026

@justinmc @gmackall I'm not sure why the Google test failed. Since I'm an external contributor, I don't have access to the test logs. Could you please take a look?

@reidbaker
Copy link
Contributor

@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.

@reidbaker reidbaker added the autosubmit Merge PR when tree becomes green via auto submit App label Feb 2, 2026
@reidbaker
Copy link
Contributor

Google3 testing failure is overridden by me. Thank you again for your contribution!

@auto-submit auto-submit bot added this pull request to the merge queue Feb 2, 2026
Merged via the queue into flutter:master with commit 50ab280 Feb 2, 2026
181 checks passed
@flutter-dashboard flutter-dashboard bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Feb 2, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 2, 2026
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 2, 2026
auto-submit bot pushed a commit to flutter/packages that referenced this pull request Feb 2, 2026
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
LongCatIsLooong pushed a commit to LongCatIsLooong/flutter that referenced this pull request Feb 6, 2026
…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
flutter-zl pushed a commit to flutter-zl/flutter that referenced this pull request Feb 10, 2026
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

engine flutter/engine related. See also e: labels. platform-android Android applications specifically team-android Owned by Android platform team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FlutterFragmentActivity support for predictive back

4 participants