You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter应用仅在Android设备旋转屏幕时出现重启问题的排查求助

Troubleshooting Android Screen Rotation Restart Issue in Flutter App

Hey there, since you're already a native Android developer, you know the core of how screen rotation works—but let's break down targeted checks to get to the bottom of why your Flutter app is restarting on Android rotation (while iOS works fine).

1. First, Confirm the Root Cause with Logs

Before diving into configs, let's verify if it's an Activity restart or a full app process restart:

  • Run adb logcat -v time in your terminal, then rotate your device.
  • Search for your app's package name (e.g., com.someapp.app). Look for logs like:
    • Destroying activity {com.someapp.app/com.someapp.app.MainActivity} (indicates Activity restart)
    • Process com.someapp.app (PID XXXX) has died (indicates full process crash/restart)
      If it's a process crash, dig into the exception stacks in the logs to pinpoint the crash source.

2. Fix the android:configChanges Placement (Critical!)

Looking at your AndroidManifest.xml, you added android:configChanges to the <application> tag—but this attribute belongs to the <activity> tag, not the application level. This is likely the main issue! Move that line into your <activity> declaration:

<activity 
    android:name=".MainActivity" 
    android:hardwareAccelerated="true" 
    android:launchMode="singleTop" 
    android:theme="@style/LaunchTheme" 
    android:windowSoftInputMode="adjustResize"
    android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode">
  • Double-check for typos in the configChanges values—even a small typo can cause the system to ignore the setting.

3. Check for Third-Party Plugin Conflicts

Your Flutter version (2.2.3 stable) is a bit older, and some plugins may not handle configuration changes properly. Try:

  • Temporarily removing all third-party plugins from your pubspec.yaml, run flutter pub get, and test rotation again.
  • If the issue disappears, re-add plugins one by one to identify the culprit. Plugins related to device sensors, UI layout, or native integrations are common suspects.

4. Verify Flutter State Management & Widget Tree Behavior

Even if the Activity doesn't restart, a poorly structured widget tree can make it feel like the app restarted:

  • Check if you're storing state in StatefulWidget instances that get rebuilt on rotation without proper retention (e.g., not using AutomaticKeepAliveClientMixin for tabs, or relying on ephemeral state instead of global managers like Provider/Riverpod).
  • Avoid heavy initialization in initState() that would reset state when the widget rebuilds (rotation triggers a widget tree rebuild even if the Activity stays alive).

5. Rule Out Android System Settings

Sometimes system-level settings override app configurations:

  • Go to Settings > Developer Options and ensure Don't keep activities is turned off. This setting forces the system to destroy activities as soon as they're backgrounded, causing restarts on rotation.
  • Temporarily disable aggressive battery saving modes—some manufacturers kill processes during configuration changes to save power.

6. Inspect Custom FlutterActivity Logic

While your MainActivity.kt is a simple subclass of FlutterActivity, double-check:

  • You haven't overridden onConfigurationChanged() without calling super.onConfigurationChanged(newConfig)—missing this breaks Flutter's rotation handling.
  • There's no custom logic in your Application class that listens to configuration changes and triggers a restart.

内容的提问来源于stack exchange,提问作者Stillie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 22:12:50