Reputation: 41
I am using Go Router for nested navigation on Flutter. The navigation works until I press the back button on the android phone then when I try to click nested navigation wrapped in bottom bar navigation it's slow to load or unclickable and I get this:
W/WindowOnBackDispatcher( 9978): sendCancelIfRunning: isInProgress=falsecallback=io.flutter.embedding.android.FlutterActivity$1@2a3415f
D/EGL_emulation( 9978): app_time_stats: avg=165.14ms min=5.89ms max=2133.05ms count=14
Also when click the android back button again it goes to the home screen.
Before I was getting this error:
OnBackInvokedCallback is not enabled for the application.
Set 'android:enableOnBackInvokedCallback="true"' in the application manifest.
So I set 'android:enableOnBackInvokedCallback="true"' in the main folder AndroidManifest.xml at application level. There are 2 other AndroidManifest.xml files in the debug and profile folder but I didn't set 'android:enableOnBackInvokedCallback="true"' in those and they also had no application level in the AndroidManifest.xml
After I set 'android:enableOnBackInvokedCallback="true"' in AndroidManifest the nested navigation continued to be unclickable after I hit the android phone backbutton but with error I mentioned before.
I have tried updating the version, upgrading AndroidX Activity and setting 'android:enableOnBackInvokedCallback="true"
// In my build.gradle file:
dependencies {
// Added this in addition to my other dependencies
implementation "androidx.activity:activity:1.6.0-alpha05"
basically I did step 1 and 2 of the answer by FGH then didn't know what to do about of what was mentioned in the rest of the steps I don't think it's relevant to my project because I don't have code for onBackPressedCallback(it might be in the background somewhere I don't know). StackOverflow Answer
I don't think the steps in the stackoverflow mentioned above with work, maybe I have to overide WindowOnBackDispatcher somehow I don't know. It looks like its because I am adding more screens to the nested navigation in the bottom navigation bar
Here is my Go Router Navigation:
// GoRouter configuration
static final GoRouter router = GoRouter(
initialLocation: initial,
debugLogDiagnostics: true,
navigatorKey: _rootNavigatorKey,
routes: [
GoRoute(
path: '/',
builder: (context, state) => WelcomeScreen(),
routes: [
GoRoute(
path: 'login',
builder: (context, state) => LoginScreen(),
routes: [
GoRoute(
path: 'register',
name: 'Register',
builder: (context, state) => RegisterScreen()),
GoRoute(
path: 'forgotPassword',
name: 'forgotPassword',
builder: (context, state) => ForgotPasswordScreen(),
routes: [
GoRoute(
path: 'OTPVerification',
name: 'OTPVerification',
builder: (context, state) => OtpVerificationScreen(),
routes: [
GoRoute(
path: 'createNewPassword',
name: 'createNewPassword',
builder: (context, state) =>
CreateNewPasswordScreen(),
routes: [
GoRoute(
path: 'passwordChanged',
name: 'passwordChanged',
builder: (context, state) =>
PasswordChangedScreen()),
],
),
]),
]),
],
),
],
),
/// MainWrapper
StatefulShellRoute.indexedStack(
builder: (context, state, navigationShell) {
return CustomBottomBar(
navigationShell: navigationShell,
);
},
branches: <StatefulShellBranch>[
/// Branch Home
StatefulShellBranch(
navigatorKey: _shellNavigatorHome,
routes: <RouteBase>[
GoRoute(
path: "/home",
name: "Home",
builder: (BuildContext context, GoRouterState state) =>
HomePage(),
routes: [
GoRoute(
path: 'routesAndGamesList',
name: 'routesAndGamesList',
pageBuilder: (context, state) => CustomTransitionPage<void>(
key: state.pageKey,
child: RoutesAndGameList(),
transitionsBuilder:
(context, animation, secondaryAnimation, child) =>
FadeTransition(opacity: animation, child: child),
),
),
GoRoute(
path: 'routesList',
name: 'routesList',
pageBuilder: (context, state) => CustomTransitionPage<void>(
key: state.pageKey,
child: RoutesList(),
transitionsBuilder:
(context, animation, secondaryAnimation, child) =>
FadeTransition(opacity: animation, child: child),
),
),
],
),
],
),
StatefulShellBranch(
navigatorKey: _shellNavigatorInfo,
routes: <RouteBase>[
GoRoute(
path: "/info",
name: "Info",
builder: (BuildContext context, GoRouterState state) =>
InfoPage(),
),
],
),
StatefulShellBranch(
navigatorKey: _shellNavigatorProfile,
routes: <RouteBase>[
GoRoute(
path: "/profile",
name: "Profile",
builder: (BuildContext context, GoRouterState state) =>
ProfilePage(),
routes: <RouteBase>[
GoRoute(
path: 'achievements',
name: 'Achievements',
pageBuilder: (context, state) => CustomTransitionPage<void>(
key: state.pageKey,
child: AchievementsScreen(),
transitionsBuilder:
(context, animation, secondaryAnimation, child) =>
FadeTransition(opacity: animation, child: child),
),
routes: [
GoRoute(
path: 'trophy',
name: 'Trophy',
builder: (context, state) => TrophyScreen(),
),
],
),
],
),
],
),
StatefulShellBranch(
navigatorKey: _shellNavigatorStories,
routes: <RouteBase>[
GoRoute(
path: "/stories",
name: "Stories",
builder: (BuildContext context, GoRouterState state) =>
StoriesPage(),
),
],
),
],
),
],
);
Upvotes: 3
Views: 2363