auto_route_library icon indicating copy to clipboard operation
auto_route_library copied to clipboard

The system back button closes the app on Android 13

Open SametSahin10 opened this issue 1 year ago • 15 comments

The system back button closes the app on Android when it's supposed go back to the previous page. The issue does not occur on Android 10. It occurs on Android 13. I have run my app on an Android 10 device and pressed the system back button. The didPopRoute on RootBackButtonDispatcher has been called. I've run the app on my Android 13 device and pressed the system back button but the didPopRoute hasn't been called. What could be the issue here?

I'm using:

auto_route: ^7.1.0
auto_route_generator: ^7.0.0

flutter doctor -v

[✓] Flutter (Channel stable, 3.7.12, on macOS 13.3.1 22E772610a darwin-arm64, locale en-TR)
    • Flutter version 3.7.12 on channel stable at /Users/sametsahin/work/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 4d9e56e694 (3 weeks ago), 2023-04-17 21:47:46 -0400
    • Engine revision 1a65d409c7
    • Dart version 2.19.6
    • DevTools version 2.20.1

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.2)
    • Android SDK at /Users/sametsahin/Library/Android/sdk
    • Platform android-33, build-tools 33.0.2
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-8887301)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.3)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14E222b
    • CocoaPods version 1.12.0

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • 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 11.0.15+0-b2043.56-8887301)

[✓] VS Code (version 1.78.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.62.0

[✓] Connected device (3 available)
    • SM G960U1 (mobile) • 395a545443503098 • android-arm64  • Android 10 (API 29)
    • macOS (desktop)    • macos            • darwin-arm64   • macOS 13.3.1 22E772610a darwin-arm64
    • Chrome (web)       • chrome           • web-javascript • Google Chrome 112.0.5615.137

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!

SametSahin10 avatar May 09 '23 21:05 SametSahin10

AlexandrMolecule avatar May 23 '23 09:05 AlexandrMolecule

Users who are having this issue, what's the value of android:launchMode in your AndroidManifext.xml? If it's singleTask, try changing it to singleTop and check if the issue gets resolved.

SametSahin10 avatar May 29 '23 14:05 SametSahin10

Are you having the issue? @AlexandrMolecule Can you try my suggestion above?

SametSahin10 avatar May 29 '23 14:05 SametSahin10

Hey @SametSahin10 is this documented somewhere or did you just tried it your self?

Milad-Akarie avatar May 29 '23 14:05 Milad-Akarie

My coworker made some changes in AndroidManifest.xml and the issue got resolved. I'm trying to isolate the change that fixed the issue.

SametSahin10 avatar May 29 '23 14:05 SametSahin10

Have the same problem, but launchMode is singleTop

zatovagul avatar Jun 13 '23 05:06 zatovagul

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions

github-actions[bot] avatar Aug 12 '23 08:08 github-actions[bot]

I have the same issue: pressing the hardware back button closes the application, and I can't intercept this behavior (Android 13.1; android:launchMode="singleTop" in AndroidManifest.xml; auto_route v6.4.0). I tried working with RootBackButtonDispatcher and AutoRouterDelegate, but debug breakpoints didn't even stop in them. Maybe I did it in the wrong place?

I tried to intercept the event here:

Widget build(BuildContext context) {
  final router = ref.watch(routerProvider).rootRouter;
  
   MaterialApp.router(
      debugShowCheckedModeBanner: false,
      routerConfig: router.config(),
      // routerConfig: RouterConfig(
      //   routeInformationProvider: router.routeInfoProvider(),
      //   routeInformationParser: router.defaultRouteParser(),
      //   routerDelegate: router.delegate(),
      // ),
   );
}

Here is how my app_router.dart looks. MainRoute is a TabsRouter.

@AutoRouterConfig(replaceInRouteName: 'View|RouterView,Route')
class AppRouter extends _$AppRouter {
  @override
  final List<AutoRoute> routes = [
    AutoRoute(
      path: '/first-page',
      page: FirstRoute.page,
      initial: !EnvConfig.mainRouteInPriority,
      guards: [LogoutGuard()],
    ),
    AutoRoute(
      path: '/local-auth',
      page: LocalAuthRoute.page,
      guards: [AuthGuard()],
      initial: EnvConfig.mainRouteInPriority,
    ),
    AutoRoute(
      path: '/main',
      page: MainRoute.page,
      guards: [AuthGuard(), PushNotificationsGuard()],
      children: [
        RedirectRoute(
          path: '',
          redirectTo: 'home',
        ),
        AutoRoute(
          path: 'home',
          page: HomeRouter.page,
          children: [
            AutoRoute(path: '', page: HomeRoute.page),
          ],
        ),
        AutoRoute(
          path: 'support',
          page: SupportRouter.page,
          children: [
            AutoRoute(
              path: '',
              page: SupportRoute.page,
            ),
            AutoRoute(
              path: 'ticket-creating-first-step',
              page: TicketCreatingFirstStepRoute.page,
            ),
            AutoRoute(
              path: 'ticket-creating-second-step',
              page: TicketCreatingSecondStepRoute.page,
            ),
            AutoRoute(
              path: 'chat',
              page: SupportChatRoute.page,
            ),
          ],
        ),
      ],
    ),
  ];
}

Main page with tabs:

Widget build(BuildContext context, ref) {
  return AutoTabsScaffold(
    routes: TabData.values.map((e) => e.route).toList(),
    bottomNavigationBuilder: (_, tabsRouter) {
      return BottomAppBar(...);
      },
  );
}

RouterView for the support tab:

@RoutePage(name: 'SupportRouter')
class SupportRouterView extends ConsumerWidget {
  const SupportRouterView({super.key});

  @override
  Widget build(BuildContext context, ref) {
    final routerKeys = ref.watch(routerProvider).tabRouterKeys;

    return AutoRouter(
      key: routerKeys[TabData.support],
    );
  }
}

vaspadi avatar Aug 17 '23 15:08 vaspadi

Facing the same issue on Android 13. Could it be related to this? https://developer.android.com/about/versions/12/behavior-changes-all#back-press

NachiketaVadera avatar Sep 01 '23 07:09 NachiketaVadera

It looks like cause of issue is the https://developer.android.com/guide/navigation/custom-back/predictive-back-gesture, you can find related discussion here https://github.com/flutter/flutter/issues/109513. As a temporary solution, I've removed android:enableOnBackInvokedCallback="true" from manifest

Kuzmenko avatar Sep 01 '23 09:09 Kuzmenko

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions

github-actions[bot] avatar Oct 06 '23 08:10 github-actions[bot]

Same problem on Android 14, reported in detail on #1788

EDIT: the problem is gone using @Kuzmenko solution

rignaneseleo avatar Nov 09 '23 12:11 rignaneseleo

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions

github-actions[bot] avatar Dec 10 '23 08:12 github-actions[bot]