From 7a660c5f34503e22322bac19ba3531145effb411 Mon Sep 17 00:00:00 2001 From: ColdPaleLight <31977171+ColdPaleLight@users.noreply.github.com> Date: Wed, 26 Jan 2022 23:26:22 +0800 Subject: [PATCH] Make sure 'FlutterSplashView.onRestoreInstanceState' doesn't crash (flutter/engine#31045) * Make sure 'FlutterSplashView.onRestoreInstanceState' doesn't crash * Add the unit test --- .../io/flutter/embedding/android/FlutterSplashView.java | 7 ++++++- .../io/flutter/embedding/android/FlutterViewTest.java | 8 ++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/engine/src/flutter/shell/platform/android/io/flutter/embedding/android/FlutterSplashView.java b/engine/src/flutter/shell/platform/android/io/flutter/embedding/android/FlutterSplashView.java index eefce9d1c8..1f6e8d49d2 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/embedding/android/FlutterSplashView.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/embedding/android/FlutterSplashView.java @@ -14,6 +14,7 @@ import android.widget.FrameLayout; import androidx.annotation.Keep; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.annotation.VisibleForTesting; import io.flutter.Log; import io.flutter.embedding.engine.FlutterEngine; import io.flutter.embedding.engine.renderer.FlutterUiDisplayListener; @@ -28,7 +29,7 @@ import io.flutter.embedding.engine.renderer.FlutterUiDisplayListener; @Nullable private SplashScreen splashScreen; @Nullable private FlutterView flutterView; @Nullable private View splashScreenView; - @Nullable private Bundle splashScreenState; + @VisibleForTesting @Nullable /* package */ Bundle splashScreenState; @Nullable private String transitioningIsolateId; @Nullable private String previousCompletedSplashIsolate; @@ -99,6 +100,10 @@ import io.flutter.embedding.engine.renderer.FlutterUiDisplayListener; @Override protected void onRestoreInstanceState(Parcelable state) { + if (!(state instanceof SavedState)) { + super.onRestoreInstanceState(state); + return; + } SavedState savedState = (SavedState) state; super.onRestoreInstanceState(savedState.getSuperState()); previousCompletedSplashIsolate = savedState.previousCompletedSplashIsolate; diff --git a/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/android/FlutterViewTest.java b/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/android/FlutterViewTest.java index ee3bad454b..1dfa6924de 100644 --- a/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/android/FlutterViewTest.java +++ b/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/android/FlutterViewTest.java @@ -955,6 +955,14 @@ public class FlutterViewTest { assertEquals(null, flutterView.findViewByAccessibilityIdTraversal(accessibilityViewId)); } + @Test + public void flutterSplashView_itDoesNotCrashOnRestoreInstanceState() { + final FlutterSplashView splashView = new FlutterSplashView(RuntimeEnvironment.application); + splashView.onRestoreInstanceState(View.BaseSavedState.EMPTY_STATE); + // It should not crash and "splashScreenState" should be null. + assertEquals(null, splashView.splashScreenState); + } + public void ViewportMetrics_initializedPhysicalTouchSlop() { FlutterView flutterView = new FlutterView(RuntimeEnvironment.application); FlutterEngine flutterEngine =