Skip to content

FlutterFragmentActivity support for predictive back #149753

Closed

Description

@justinmc

Internal: b/242216228

Use case

Several popular pub packages (local_auth, health) require the use of FlutterFragmentActivity in apps. Apps that use those packages should be able to support predictive back, too.

Proposal

Predictive back support was added to FlutterActivity in flutter-team-archive/engine#52302 and to FlutterFragment in flutter-team-archive/engine#52302. Support should also be added to FlutterFragmentActivity, including support for route transitions (flutter-team-archive/engine#49093).

What is predictive back?

Gif

217918109-945febaa-9086-41cc-a476-1a189c7831d8

What are predictive back route transitions?

Video
predictive_back.mp4

Activity

  1. added
    P2Important issues not at the top of the work list
    on Jun 5, 2024
  2. roberto-gigli commented on Jun 7, 2024

    @roberto-gigli

    I'm experiencing the same issue, and can't add predictive back if i use local auth in my projects

  3. NeKoFu commented on Sep 3, 2024

    @NeKoFu

    The package flutter_stripe also requires FlutterFragmentActivity. So I'm stuck with Flutter v3.22.3. Our project is totally crappy without back gesture since the "Stable" version of Flutter v3.24.1. 😥
    I understand that you must be very busy, but do you have an ETA for an update? 😅

  4. justinmc commented on Sep 3, 2024

    @justinmc
    ContributorAuthor

    If I try to just change FlutterActivity to FlutterFragmentActivity in MainActivity.kt it seems to be working, am I missing something? Predictive back works when leaving the app from the root route. PredictiveBackPageTransition doesn't work, but it correctly defaults to ZoomPageTransition and back gestures work.

    class MainActivity: FlutterFragmentActivity() {
    }

    @NeKoFu what is your setup like? Can you try on the master branch?

    Maybe I did enable this in FlutterFragmentActivity here? https://github.com/flutter/engine/pull/52302/files#diff-893547c230a70f1b0618f0b79a56c67d8506f0983a5a92b819f13c1071fd7d84

    Example app that I used, just navigation nothing fancy
    import 'package:flutter/material.dart';
    
    void main() async {
      runApp(const _MyApp());
    }
    
    class _MyApp extends StatelessWidget {
      const _MyApp();
    
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          restorationScopeId: 'root',
          theme: ThemeData(
            pageTransitionsTheme: const PageTransitionsTheme(
              builders: {
                TargetPlatform.android: PredictiveBackPageTransitionsBuilder(),
              },
            ),
          ),
          title: 'Flutter Demo',
          initialRoute: '/',
          routes: <String, WidgetBuilder>{
            '/': (BuildContext context) {
              return const MyHomePage();
            },
            'leaf': (BuildContext context) {
              return const LeafPage();
            },
          },
        );
      }
    }
    
    class MyHomePage extends StatelessWidget {
      const MyHomePage({super.key});
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                ElevatedButton(
                  child: const Text('Open next page'),
                  onPressed: () => Navigator.of(context).restorablePushNamed('leaf'),
                ),
              ],
            ),
          ),
        );
      }
    }
    
    class LeafPage extends StatelessWidget {
      const LeafPage({super.key});
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              ElevatedButton(
                child: const Text('Open modal'),
                onPressed: () => showModalBottomSheet<void>(
                  context: context,
                  useRootNavigator: true,
                  builder: (context) => const Padding(
                    padding: EdgeInsets.all(24),
                    child: Text(
                      'Close this modal and then use system back nav, '
                      'which will unexpectedly close the app.',
                    ),
                  ),
                ),
              ),
              const SizedBox(height: 16, width: double.infinity),
              ElevatedButton(
                child: const Text('Open dialog'),
                onPressed: () => showDialog<void>(
                  context: context,
                  useRootNavigator: true,
                  builder: (context) => const AlertDialog(
                    contentPadding: EdgeInsets.all(24),
                    content: Text(
                      'Close this dialog and then use system back nav, '
                      'which will unexpectedly close the app.',
                    ),
                  ),
                ),
              ),
            ],
          ),
        );
      }
    }
  5. NeKoFu commented on Sep 4, 2024

    @NeKoFu

    @justinmc I'm using go_router for navigation, and since the flutter_stripe package requires FlutterFragmentActivity, I had to switch to that. With Flutter 3.22.3, the back gesture worked as expected, returning to the previous page. However, after upgrading to Flutter 3.24.1 (stable channel), the back gesture now closes the app instead of navigating back.

    I will try with the master branch tomorrow 😉

  6. NeKoFu commented on Sep 8, 2024

    @NeKoFu

    @justinmc To isolate the issue, I had created, like you, a minimal project with navigation between two pages using go_router. And indeed, good news, this minimal project is working again using the master branch and your commits related to FlutterFragmentActivity. 😊

    However, 😥 I was still facing the problem on my actual application project. So, I took some time to investigate, and what I found is that if a function is attached to the onNavigationNotification attribute in MaterialApp.router(), then the Back Gesture consistently closes the application. 🤔

    import 'package:flutter/material.dart';
    import 'package:navtest/route.dart';
    
    void main() {
      runApp(const MyApp());
    }
    
    class MyApp extends StatelessWidget {
      const MyApp({super.key});
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp.router(
          title: 'Flutter Demo',
          theme: ThemeData(
            colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
            useMaterial3: true,
          ),
          routerConfig: AppRoutes.router,
          // onNavigationNotification: (NavigationNotification notification) => true, // <<-- break the fix and close the app on back gesture
        );
      }
    }
  7. AlexDochioiu commented on Oct 20, 2024

    @AlexDochioiu

    @darshankawar @justinmc Any chance this can be bumped to P1? Entire app navigation seems to be really broken in many ways for apps using FlutterFragmentActivity (which is also unavoidable for many projects) and disabling predictive back on android manifest doesn't seem to fix it anymore starting with flutter 3.24. Both PopScope and the old WillPopScope don't work as intended at all.

  8. justinmc commented on Oct 22, 2024

    @justinmc
    ContributorAuthor

    Thanks for everyone providing context here. I'm trying to find the bandwidth for this and will post back. If anyone else wants to submit a PR, I'll assign myself if I start working on this, otherwise it's free for anyone to work on it.

  9. AlexDochioiu commented on Oct 29, 2024

    @AlexDochioiu

    For anyone having the same issue, this workaround fixes navigation the navigation for Flutter 3.24.x : Milad-Akarie/auto_route_library#2059 (comment)

  10. bazl-E commented on Nov 5, 2024

    @bazl-E

    sample project to reproduce the issue https://github.com/safaIocod/iso_sample

    Steps to reproduce
    1- Run the project
    2- Switch the tab using the bottom navigation bar
    3- go to the inner page
    4 - press the back button (will redirect to the home page),
    5 - press the back button once more (the app will close)

    expected result
    As per the code , on the step 5 app should go to the 1st tab in the bottom navigation bar , again pressing back button from there should show a confirmation dialogue

  11. backids99 commented on Dec 1, 2024

    @backids99

    For anyone having the same issue, this workaround fixes navigation the navigation for Flutter 3.24.x : Milad-Akarie/auto_route_library#2059 (comment)

    Hi @AlexDochioiu, after i trying your reference but still not working for me. I try routing using https://docs.flutter.dev/cookbook/navigation/named-routes not working, and i try using other way https://pub.dev/packages/go_router still not working to using system navigation back for pop route.

    flutter doctor:
    [√] Flutter (Channel stable, 3.24.4, on Microsoft Windows [Version 10.0.26100.2454], locale en-US)
        • Flutter version 3.24.4 on channel stable at C:\Flutter\flutter
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision 603104015d (5 weeks ago), 2024-10-24 08:01:25 -0700
        • Engine revision db49896cf2
        • Dart version 3.5.4
        • DevTools version 2.37.3
    
    [√] Windows Version (Installed version of Windows is version 10 or higher)
    
    [√] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
        • Android SDK at C:\Users\***\AppData\Local\Android\sdk
        • Platform android-35, build-tools 35.0.0
        • Java binary at: C:\Users\***\.jdks\corretto-17.0.13\bin\java
        • Java version OpenJDK Runtime Environment Corretto-17.0.13.11.1 (build 17.0.13+11-LTS)
        • All Android licenses accepted.
    
    [√] Chrome - develop for the web
        • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe
    
    [√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.11.5)
        • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community
        • Visual Studio Community 2022 version 17.11.35327.3
        • Windows 10 SDK version 10.0.22621.0
    
    [√] Android Studio (version 2024.2)
        • Android Studio at C:\Program Files\Android\Android Studio
        • Flutter plugin can be installed from:
           https://plugins.jetbrains.com/plugin/9212-flutter
        • Dart plugin can be installed from:
           https://plugins.jetbrains.com/plugin/6351-dart
        • Java version OpenJDK Runtime Environment (build 21.0.3+-12282718-b509.11)
    
    [√] IntelliJ IDEA Community Edition (version 2024.2)
        • IntelliJ at C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2024.2.4
        • Flutter plugin can be installed from:
           https://plugins.jetbrains.com/plugin/9212-flutter
        • Dart plugin can be installed from:
           https://plugins.jetbrains.com/plugin/6351-dart
    
    [√] VS Code (version 1.95.3)
        • VS Code at C:\Users\***\AppData\Local\Programs\Microsoft VS Code
        • Flutter extension version 3.100.0
    
    [√] Connected device (4 available)
        • sdk gphone64 x86 64 (mobile) • emulator-5554 • android-x64    • Android 15 (API 35) (emulator)
        • Windows (desktop)            • windows       • windows-x64    • Microsoft Windows [Version 10.0.26100.2454]
        • Chrome (web)                 • chrome        • web-javascript • Google Chrome 131.0.6778.86
        • Edge (web)                   • edge          • web-javascript • Microsoft Edge 130.0.2849.80
    
    [√] Network resources
        • All expected network resources are available.
    
    • No issues found!
    
  12. ZhuJHua commented on Dec 1, 2024

    @ZhuJHua

    For anyone having the same issue, this workaround fixes navigation the navigation for Flutter 3.24.x : Milad-Akarie/auto_route_library#2059 (comment)

    Hi @AlexDochioiu, after i trying your reference but still not working for me. I try routing using https://docs.flutter.dev/cookbook/navigation/named-routes not working, and i try using other way https://pub.dev/packages/go_router still not working to using system navigation back for pop route.

    flutter doctor:

    [√] Flutter (Channel stable, 3.24.4, on Microsoft Windows [Version 10.0.26100.2454], locale en-US)
        • Flutter version 3.24.4 on channel stable at C:\Flutter\flutter
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision 603104015d (5 weeks ago), 2024-10-24 08:01:25 -0700
        • Engine revision db49896cf2
        • Dart version 3.5.4
        • DevTools version 2.37.3
    
    [√] Windows Version (Installed version of Windows is version 10 or higher)
    
    [√] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
        • Android SDK at C:\Users\***\AppData\Local\Android\sdk
        • Platform android-35, build-tools 35.0.0
        • Java binary at: C:\Users\***\.jdks\corretto-17.0.13\bin\java
        • Java version OpenJDK Runtime Environment Corretto-17.0.13.11.1 (build 17.0.13+11-LTS)
        • All Android licenses accepted.
    
    [√] Chrome - develop for the web
        • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe
    
    [√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.11.5)
        • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community
        • Visual Studio Community 2022 version 17.11.35327.3
        • Windows 10 SDK version 10.0.22621.0
    
    [√] Android Studio (version 2024.2)
        • Android Studio at C:\Program Files\Android\Android Studio
        • Flutter plugin can be installed from:
           https://plugins.jetbrains.com/plugin/9212-flutter
        • Dart plugin can be installed from:
           https://plugins.jetbrains.com/plugin/6351-dart
        • Java version OpenJDK Runtime Environment (build 21.0.3+-12282718-b509.11)
    
    [√] IntelliJ IDEA Community Edition (version 2024.2)
        • IntelliJ at C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2024.2.4
        • Flutter plugin can be installed from:
           https://plugins.jetbrains.com/plugin/9212-flutter
        • Dart plugin can be installed from:
           https://plugins.jetbrains.com/plugin/6351-dart
    
    [√] VS Code (version 1.95.3)
        • VS Code at C:\Users\***\AppData\Local\Programs\Microsoft VS Code
        • Flutter extension version 3.100.0
    
    [√] Connected device (4 available)
        • sdk gphone64 x86 64 (mobile) • emulator-5554 • android-x64    • Android 15 (API 35) (emulator)
        • Windows (desktop)            • windows       • windows-x64    • Microsoft Windows [Version 10.0.26100.2454]
        • Chrome (web)                 • chrome        • web-javascript • Google Chrome 131.0.6778.86
        • Edge (web)                   • edge          • web-javascript • Microsoft Edge 130.0.2849.80
    
    [√] Network resources
        • All expected network resources are available.
    
    • No issues found!
    

    Did you use the Getx as route manager?

  13. backids99 commented on Dec 1, 2024

    @backids99

    Hi @ZhuJHua, sorry but I don't use Getx...do you have any ideas? I have to use FlutterFragmentActivity for local_auth, but after changing from FlutterActivity i have a problem cannot back (using system back button) to previous page and app will minimize after click system back button. I think you have same problem but unlucky i cannot fixing with this solution (Milad-Akarie/auto_route_library#2059 (comment)) to my project.

  14. 4 remaining items

  15. AlexDochioiu commented on Jan 10, 2025

    @AlexDochioiu

    Any updates on this? Proper support for FlutterFragmentActivity is very important since important features like biometrics login cannot work without it.

  16. kevinhuijsman13 commented on Jan 16, 2025

    @kevinhuijsman13

    Any updates on this?

  17. FrankenApps commented on May 25, 2025

    @FrankenApps
    Contributor

    I am not entirely sure, if I do completly understand this issue. However the problems previously experienced with back button handling in a FlutterFragementActivity after upgrading to Flutter 3.22 seem to be resolved with the release of Flutter 3.32 (probably by #161545).

  18. EArminjon commented on May 25, 2025

    @EArminjon
    Contributor

    I am not entirely sure, if I do completly understand this issue. However the problems previously experienced with back button handling in a FlutterFragementActivity after upgrading to Flutter 3.22 seem to be resolved with the release of Flutter 3.32 (probably by #161545).

    I'm using auto_route, predictive back gesture on my main screen well work using3.32 but not the others (created by .push). Does your routes works ?

  19. anders-au commented on Nov 6, 2025

    @anders-au

    I am not entirely sure, if I do completly understand this issue. However the problems previously experienced with back button handling in a FlutterFragementActivity after upgrading to Flutter 3.22 seem to be resolved with the release of Flutter 3.32 (probably by #161545).

    This remains an issue as of Flutter 3.35.7.

    It works when exiting the app, but internal page transitions are not using Predictive Back. Unfortunately libraries like RevenueCat require FlutterFragementActivity, so I cannot switch.

    Has anyone found a workaround for this?

  20. AlexDochioiu commented on Nov 6, 2025

    @AlexDochioiu

    I use this approach. Navigation seems fine with it

    final materialApp = MaterialApp(
          // FLUTTER 3.24.x and newer (when using FlutterFragmentActivity)
          // android navigation bugs due to predictive back gesture
          // https://github.com/flutter/flutter/issues/149753
          // https://github.com/Milad-Akarie/auto_route_library/issues/1788
          // https://github.com/flutter/flutter/issues/145290
          // https://github.com/flutter/flutter/issues/138614#issuecomment-1909155402
          // https://github.com/flutter/flutter/issues/145159
          // https://github.com/flutter/flutter/issues/138737
          // https://github.com/flutter/flutter/issues/155441
          //
          // Note, we cannot get rid of FlutterFragmentActivity because it is required for local auth to work
          onNavigationNotification: UniversalPlatform.isAndroid
              ? (notification) {
                  switch (WidgetsBinding.instance.lifecycleState) {
                    case null:
                    case AppLifecycleState.detached:
                    case AppLifecycleState.inactive:
                      // Avoid updating the engine when the app isn't ready.
                      return true;
                    case AppLifecycleState.resumed:
                    case AppLifecycleState.hidden:
                    case AppLifecycleState.paused:
                      SystemNavigator.setFrameworkHandlesBack(true);
    
                      /// This must be `true` instead of `notification.canHandlePop`, otherwise application closes on back gesture.
                      return true;
                  }
                }
              : null,
  21. anders-au commented on Nov 7, 2025

    @anders-au

    I use this approach. Navigation seems fine with it

    Thanks for this, but this did not work for me.

    Are there any other steps that you had to apply to get this to work (aside from the pageTransitionsTheme and AndroidManifest code changes).

    To confirm it wasn't something else in my config preventing it, I just switched back to FlutterActivity and predictive back worked.

    (Just to clarify, I am talking about predictive back within the app, where you can see the previous screen mid-gesture.)

  22. AlexDochioiu commented on Nov 7, 2025

    @AlexDochioiu

    @anders-au Those changes are meant to properly disable predictive gestures (I think also in android manifest you'd need to do it) because it breaks app navigation in flutter.

    To my knowledge, you cannot possibly use predictive gestures with FlutterFragementActivity without breaking the navigation.

    Your only choice are:

    1. Use FlutterFragementActivity and fully disable predictive gestures
    2. Use FlutterActivity to be able to keep predictive gestures, but ofc this is not possible with some libraries.

    I never found any workaround to use FlutterFragementActivity with predictive gestures.

  23. added a commit that references this issue on Feb 2, 2026
    50ab280
  24. added a commit that references this issue on Feb 6, 2026
    ab61b2f
  25. added a commit that references this issue on Feb 10, 2026
    162cd6e
  26. github-actions commented on Feb 16, 2026

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  27. locked as resolved and limited conversation to collaborators on Feb 16, 2026
  28. added a commit that references this issue on Feb 19, 2026
    ababbe7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listteam-text-inputOwned by Text Input teamtriaged-text-inputTriaged by Text Input team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions