Milad-Akarie / auto_route_library

Flutter route generator
MIT License
1.6k stars 405 forks source link

The app unfortunately closes on Android when using Predictive Back or the hardware back button #2059

Closed Jasco07122021 closed 2 weeks ago

Jasco07122021 commented 2 months ago

The app unfortunately closes on Android when using Predictive Back or the hardware back button

I have 5 tabs and I use AutoTabsScaffold to manage them. Whenever I tap on the second or other tab, the hardware back button takes me back to the first tab. But, after pressing the back button, the app exits. Need fix!

This issue started after I upgraded my Flutter version to 3.24

cdobrovolny commented 1 month ago

having the same issue.

AlexDochioiu commented 1 month ago

Same issue here

cdobrovolny commented 1 month ago

a valid workaround is to downgrade the Flutter-Sdk to 3.22.0

xVemu commented 1 month ago

Current workaround is to pass custom onNavigationNotification in MaterialApp.router constructor.


MaterialApp.router(
  onNavigationNotification: _defaultOnNavigationNotification,
);

bool _defaultOnNavigationNotification(NavigationNotification _) {
  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;
  }
}
ddfreiling commented 1 month ago

Seems related to https://github.com/flutter/flutter/issues/156517

GregUAs commented 1 month ago

With Flutter 3.24.3, all versions above 8.1.3 are defective for the back gesture in Android.

TiesPol commented 1 month ago

Same issue here, only when using FlutterFragmentActivity

GregUAs commented 1 month ago

Should I pass through something else? @TiesPol

LiamMarega commented 1 month ago

Any solution?

AlexDochioiu commented 1 month ago

Any solution?

This one seems to work: https://github.com/Milad-Akarie/auto_route_library/issues/2059#issuecomment-2434908775

LiamMarega commented 2 weeks ago

I have another problem with this... Now i have a endDrawer in my app like this:

 return AutoTabsScaffold(
      key: const Key('home_page_router_key'),
      animationCurve: Curves.ease,
      animationDuration: const Duration(milliseconds: 150),
      lazyLoad: true,
      primary: true,
      homeIndex: 1,
      routes: const [
        NewsHomeRoute(),
        FeedDiscoverHomeRoute(),
        GamificationRankingRoute(),
        ProfileRoute(),
      ],
      endDrawer: FeroxLoggedProfileDrawer(),
      endDrawerEnableOpenDragGesture: draggableEndDrawer,
...

But when i use Android back button this The drawer does not close until it reaches the initial route, regardless of whether it was opened on another route.

xVemu commented 2 weeks ago

Why closed?

GregUAs commented 2 weeks ago

Any solution?

This one seems to work: #2059 (comment)

It's not a good practice to manage manually all states of lifecycle. It's work but not recommended at all. Take care when you will update your project or dependencies.

AlexDochioiu commented 2 weeks ago

Any solution?

This one seems to work: #2059 (comment)

It's not a good practice to manage manually all states of lifecycle. It's work but not recommended at all. Take care when you will update your project or dependencies.

There's no management of states though, just an action performed on given lifecycle notifications (in this case to override the buggy "preferred" back button handling). While overriding the back button handling delegation may not be great, it's much better than having a buggy app navigation.

P.s. by all means, when the flutter team adds predictive back support for FlutterFragmentActivity, this workaround should be removed.