從已棄用的螢幕閃爍頁 API 遷移

在 Flutter 2.5 版本之前,Flutter 應用程式可以透過定義 manifest 檔案 (AndroidManifest.xml) 中的元資料,或在其 FlutterActivity 中實現 provideSplashScreen,再或者兩者兼顧使用,來新增螢幕閃爍頁。螢幕閃爍頁會在 Android 啟動頁顯示後,Flutter 首幀繪製前短暫顯示。從 Flutter 2.5 版本起,這種方式已被棄用。 Flutter 現在會自動保持顯示 Android 啟動頁,直至首幀繪製。

請按照 Flutter 2.5 版本之前自定義應用程式螢幕閃爍頁相應的操作步驟,將自定義的螢幕閃爍頁遷移到只用自定義應用程式的啟動頁。

FlutterActivity 中自定義螢幕閃爍頁

  1. 在應用程式的 FlutterActivity 中找到 provideSplashScreen() 方法的實現並 將其刪除。該方法應該包括了將應用程式的自定義螢幕閃爍頁建立成一個 Drawable 的實現。例如:

    @Override
    public SplashScreen provideSplashScreen() {
        // ...
        return new DrawableSplashScreen(
            new SomeDrawable(
                ContextCompat.getDrawable(this, R.some_splash_screen)));
    }
    
  2. 請按照接下來章節中的步驟,確保你的 Drawable 螢幕閃爍頁(上個步驟範例中的 R.some_splash_screen)已正確設定為應用程式的自定義啟動頁。

在 Manifest 中自定義螢幕閃爍頁

  1. 在應用程式的 AndroidManifest.xml 檔案中找到 activity 元素。在此元素中,找到 android:theme 屬性以及定義螢幕閃爍頁為 io.flutter.embedding.android.SplashScreenDrawablemeta-data 元素,以便在後續的步驟中進行更新。例如:

    <activity
        // ...
        android:theme="@style/SomeTheme">
      // ...
      <meta-data
          android:name="io.flutter.embedding.android.SplashScreenDrawable"
          android:resource="@drawable/some_splash_screen"
          />
    </activity>
    
  2. 如果未指定 android:theme 屬性,請新增該屬性併為應用程式的啟動頁 定義啟動主題

  3. 刪除 meta-data 元素,因為 Flutter 不再使用該元素,如果保留它可能會導致崩潰。

  4. 在應用程式的 style 資源中找到由 android:theme 屬性指定的主題。該主題指定了應用程式的啟動主題。請確保 styleandroid:windowBackground 屬性設定為你的自定義螢幕閃爍頁。例如:

    <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>