State Not Reset When User Force Quits the App on iOS
- Dominant language
- Dart
- Stars
- 179k
- Forks
- 31.1k
- PR merge metrics
- PR metrics pending
Description
### Steps to reproduce
On iOS, when the user force quits the app (by swiping it away from the app switcher) and then reopens it, the previous state persists, even though state restoration is expected to be cleared. This behavior does not happen on Android.
### Expected results
• When the app is force-quit by the user and restarted, it should launch with a fresh state (e.g., return to the login screen if applicable).
• The navigation stack should be cleared.
• Any restoration data should be flushed upon app termination.
### Actual results
• On iOS, reopening the app restores the previous state instead of starting fresh.
• The navigation stack does not reset even when calling GoRouter.of(context).go(/login), ServicesBinding.instance.restorationManager.flushData() ,RestorationManager().flushData(); inside onDetach.
This behavior happens with GoRouter and with Navigator
I have tried different configurations to solve this issue, but none of them worked:
Tried moving the RestorationScope widget to different locations, tried without RestorationScope widget. Tried calling flushData() in different places, including:
• Inside onDetach
• Inside dispose
### Code sample
Code sample
```dart
[ void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(const AppLifecycleManager(child: MyApp()));
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp.router(
restorationScopeId: 'app',
routerConfig: GoRouter(
initialLocation: '/',
routes: [
GoRoute(path: '/', builder: (context, state) => const HomeScreen()),
GoRoute(path: '/login', builder: (context, state) => const LoginScreen()),
],
),
);
}
}
class AppLifecycleManager extends StatefulWidget {
final Widget child;
const AppLifecycleManager({required this.child, super.key});
@override
State createState() => _AppLifecycleManagerState();
}
class _AppLifecycleManagerState extends State {
late final AppLifecycleListener _listener;
@override
void initState() {
super.initState();
_listener = AppLifecycleListener(
onDetach: () {
// Attempts to reset state restoration (it shouldn't be necessary)
GoRouter.of(navigatorKey.currentContext!).go('/login');
ServicesBinding.instance.restorationManager.flushData();should't be necessary)
RestorationManager().flushData();
},
);
}
@override
Widget build(BuildContext context) => widget.child;
}
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
title: const Text("Home"),
),
body: Center(
child: ElevatedButton(
onPressed: () {
debugPrint('navigate to login');
context.push('/login');
},
child: const Text("Go to Login"),
),
),
);
}
class LoginScreen extends StatefulWidget {
const LoginScreen({super.key});
@override
State createState() => _LoginScreenState();
}
class _LoginScreenState extends State {
String _permissionStatus = "Unknown";
Future _requestCameraPermission() async {
final status = await Permission.camera.request();
setState(() {
_permissionStatus = status.isGranted
? "Camera permission granted ✅"
: "Camera permission denied ❌";
});
if (status.isDenied || status.isPermanentlyDenied) {
}
}
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: const Text("Login")),
body: Column(
spacing: 10,
children: [
Center(child: const Text("Login Screen")),
const SizedBox(height: 20),
ElevatedButton(
onPressed: _requestCameraPermission,
child: const Text("Request Camera Permission"),
),
// Disabled the camera when it's unable to kill the app by OS
ElevatedButton(
onPressed: openAppSettings,
child: const Text("Open Settings"),
),
],
),
);
}
]
```
### Screenshots or Video
Screenshots / Video demonstration
[Upload media here]
### Logs
Logs
```console
[Paste your logs here]
```
### Flutter Doctor output
Doctor output
```console
[ [✓] Flutter (Channel stable, 3.27.4, on macOS 15.3.1 24D70 darwin-arm64, locale fr-CA)
• Flutter version 3.27.4 on channel stable at /Users/mathisdesmeules/fvm/versions/3.27.4
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision d8a9f9a52e (4 weeks ago), 2025-01-31 16:07:18 -0500
• Engine revision 82bd5b7209
• Dart version 3.6.2
• DevTools version 2.40.3
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
• Android SDK at /Users/mathisdesmeules/Library/Android/sdk
• Platform android-35, build-tools 34.0.0
• ANDROID_SDK_ROOT = /Users/mathisdesmeules/Library/Android/sdk
• Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11572160)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 16.2)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Build 16C5032a
• CocoaPods version 1.16.2
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 2023.3)
• 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 17.0.10+0-17.0.10b1087.21-11572160)
[✓] IntelliJ IDEA Ultimate Edition (version 2024.1.2)
• IntelliJ at /Users/mathisdesmeules/Applications/IntelliJ IDEA Ultimate.app
• 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.97.0)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.104.0
[✓] Connected device (6 available)
• SM A155M (mobile) • RF8XC0SK0HZ • android-arm64 • Android 14 (API 34)
• Galaxy s10 (mobile) • 00008101-000C39D821F0001E • ios • iOS 18.3.1 22D72
• iPhone 16 (mobile) • 286DDF34-0024-4D50-8DDA-FD58730D5085 • ios •
com.apple.CoreSimulator.SimRuntime.iOS-18-2 (simulator)
• macOS (desktop) • macos • darwin-arm64 • macOS 15.3.1 24D70 darwin-arm64
• Mac Designed for iPad (desktop) • mac-designed-for-ipad • darwin • macOS 15.3.1 24D70 darwin-arm64
• Chrome (web) • chrome • web-javascript • Google Chrome 133.0.6943.142
[✓] Network resources
• All expected network resources are available.
• No issues found!]
```
Contributor guide
Assessment
This issue has not been assessed yet.