[cherrypick-stable]Skip unexpected events in MultiPlatformViewBackgroundForegroundScenar… (#49247)

…io (#48456)

Fixes https://github.com/flutter/flutter/issues/138193.

Was first attempted to fix in https://github.com/flutter/engine/pull/48096, but that was not reliable since it's all asynchronous. 

-------------------------------------------

cherry pick to fix mac unopt failures on stable branch: 
https://flutter-dashboard.appspot.com/#/build?repo=engine&branch=flutter-3.16-candidate.0
diff --git a/testing/scenario_app/lib/src/platform_view.dart b/testing/scenario_app/lib/src/platform_view.dart
index 7c15465..92679d2 100644
--- a/testing/scenario_app/lib/src/platform_view.dart
+++ b/testing/scenario_app/lib/src/platform_view.dart
@@ -510,6 +510,13 @@
     PlatformMessageResponseCallback? callback,
   ) {
     final String message = utf8.decode(data!.buffer.asUint8List());
+
+    // The expected first event should be 'AppLifecycleState.resumed', but
+    // occasionally it will receive 'AppLifecycleState.inactive' first. Skip
+    // any messages until 'AppLifecycleState.resumed' is received.
+    if (_lastLifecycleState.isEmpty && message != 'AppLifecycleState.resumed') {
+      return;
+    }
     if (_lastLifecycleState == 'AppLifecycleState.inactive' &&
         message == 'AppLifecycleState.resumed') {
       _nextFrame = _secondFrame;