已棄用的啟動屏 API 遷移
在 Flutter 2.5 之前,Flutter 應用可以透過在其應用清單檔案 (AndroidManifest.xml) 的元資料中定義啟動屏,透過在其 FlutterActivity 中實現 provideSplashScreen,或兩者兼而有之,來新增啟動屏。這會在 Android 啟動屏顯示到 Flutter 繪製第一幀的短暫時間內顯示。此方法自 Flutter 2.5 起已被棄用。Flutter 現在會自動保持 Android 啟動屏顯示,直到繪製第一幀。
要從定義自定義啟動屏遷移到僅為您的應用定義自定義啟動屏,請按照您應用自定義啟動屏在 2.5 版本釋出之前是如何定義的步驟進行操作。
在 FlutterActivity 中定義的自定義啟動屏
找到您應用中
FlutterActivity內的provideSplashScreen()實現,然後刪除它。此實現應涉及將您應用的自定義啟動屏構建為Drawable。例如java@Override public SplashScreen provideSplashScreen() { // ... return new DrawableSplashScreen( new SomeDrawable( ContextCompat.getDrawable(this, R.some_splash_screen))); }請使用後續章節中的步驟,以確保您的
Drawable啟動屏(在上例中為R.some_splash_screen)已正確配置為您應用的自定義啟動屏。
在清單檔案中定義的自定義啟動屏
找到您應用的
AndroidManifest.xml檔案。在此檔案中,找到activity元素。在此元素內,識別android:theme屬性和定義啟動屏為io.flutter.embedding.android.SplashScreenDrawable的meta-data元素,並更新它。例如xml<activity // ... android:theme="@style/SomeTheme"> // ... <meta-data android:name="io.flutter.embedding.android.SplashScreenDrawable" android:resource="@drawable/some_splash_screen" /> </activity>如果未指定
android:theme屬性,請新增該屬性併為應用的啟動屏定義啟動主題。刪除
meta-data元素,因為 Flutter 不再使用它,但它可能會導致崩潰。在您應用的
style資源中,找到由android:theme屬性指定的the theme 定義。此主題指定了您應用的啟動主題。確保style屬性配置了android:windowBackground屬性,其中包含您的自定義啟動屏。例如xml<resources> <style name="SomeTheme" // ... > <!-- Show a splash screen on the activity. Automatically removed when Flutter draws its first frame --> <item name="android:windowBackground">@drawable/some_splash_screen</item> </style> </resources>