From a9c9177d358b3ff237e220e5cbdbb8f4c7f0138e Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Wed, 15 Nov 2023 09:10:49 -0800 Subject: [PATCH] In the scenario game loop test, cancel the results writer task if the activity has been destroyed (flutter/engine#48051) This test is run on Firebase Test Lab, and crashes have been reported on CI if this task executes when the activity is in a destroyed state. See https://github.com/flutter/flutter/issues/138451 --- .../dev/flutter/scenarios/TestActivity.java | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/engine/src/flutter/testing/scenario_app/android/app/src/main/java/dev/flutter/scenarios/TestActivity.java b/engine/src/flutter/testing/scenario_app/android/app/src/main/java/dev/flutter/scenarios/TestActivity.java index 1aafd660b0..79d3615f78 100644 --- a/engine/src/flutter/testing/scenario_app/android/app/src/main/java/dev/flutter/scenarios/TestActivity.java +++ b/engine/src/flutter/testing/scenario_app/android/app/src/main/java/dev/flutter/scenarios/TestActivity.java @@ -37,6 +37,18 @@ import java.util.concurrent.atomic.AtomicBoolean; public abstract class TestActivity extends TestableFlutterActivity { static final String TAG = "Scenarios"; + private Runnable resultsTask = + new Runnable() { + @Override + public void run() { + final Uri logFileUri = getIntent().getData(); + writeTimelineData(logFileUri); + testFlutterLoaderCallbackWhenInitializedTwice(); + } + }; + + private Handler handler = new Handler(); + @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -47,22 +59,18 @@ public abstract class TestActivity extends TestableFlutterActivity { if (Build.VERSION.SDK_INT > 22) { requestPermissions(new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1); } - final Uri logFileUri = launchIntent.getData(); - new Handler() - .postDelayed( - new Runnable() { - @Override - public void run() { - writeTimelineData(logFileUri); - testFlutterLoaderCallbackWhenInitializedTwice(); - } - }, - 20000); + handler.postDelayed(resultsTask, 20000); } else { testFlutterLoaderCallbackWhenInitializedTwice(); } } + @Override + protected void onDestroy() { + handler.removeCallbacks(resultsTask); + super.onDestroy(); + } + @Override @NonNull public FlutterShellArgs getFlutterShellArgs() {