I am new to Flutter and I am having a an issue on my Flutter app.
Navigator.pop(context);
is not working with the android system back navigation button press. When I use a custom button on the screen and pop, it’s navigate back without any issue, but when I use the system back navigation pressed, app gets minimized instead of navigate back.
I would like to know what I am doing wrong.
Setup,
• Running on Android 14 emulator.
• Flutter 3.24.2
• Dart 3.5.2
• DevTools 2.37.2
on AndroidManifest.xml enabled enableOnBackInvokedCallback too
<activity
android:requestLegacyExternalStorage="true"
android:usesCleartextTraffic="true"
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTop"
android:taskAffinity=""
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize"
android:enableOnBackInvokedCallback="true">
An example of my implementation
void _onBackPressed() {
Navigator.pop(context);
}
Widget build(BuildContext context) {
return PopScope(
canPop: false,
onPopInvokedWithResult: (didPop, result) {
if (didPop) return;
_onBackPressed();
// print('navigate_back : $didPop --- $result');
},
child: Scaffold(
appBar: CommonHeader(
isWP: false,
titleText: AppStrings.beCalm,
onBackPressed: _onBackPressed,
),
body: Container(
Back navigation from CommonHeader
model is working, and system back button press is not working.
Looking for your help to identify the issue.
Thanks in advance
2
Answers
The problem is this line –>
return PopScope(canPop: false,
This will not allow the android back button to navigate back. You need to either remove this widget or set the canPop to true. Now if you want to use as false on say IOS and not on android you can simply check it withPlatform.isAndroid ? true : false
which is in yourimport 'dart:io'
.and also remove the
canPop