Skip unexpected events in MultiPlatformViewBackgroundForegroundScenario (#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. 

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
diff --git a/testing/scenario_app/lib/src/platform_view.dart b/testing/scenario_app/lib/src/platform_view.dart
index d232546..e2e8d8a 100644
--- a/testing/scenario_app/lib/src/platform_view.dart
+++ b/testing/scenario_app/lib/src/platform_view.dart
@@ -422,6 +422,7 @@
     required this.secondId,
   }) {
     _nextFrame = _firstFrame;
+    channelBuffers.setListener('flutter/lifecycle', _onPlatformMessage);
   }
 
   /// The platform view identifier to use for the first platform view.
@@ -437,13 +438,7 @@
     _nextFrame();
   }
 
-  bool _firstFrameBegan = false;
-
   void _firstFrame() {
-    if (!_firstFrameBegan) {
-      channelBuffers.setListener('flutter/lifecycle', _onPlatformMessage);
-    }
-    _firstFrameBegan = true;
     final SceneBuilder builder = SceneBuilder();
 
     builder.pushOffset(50, 600);
@@ -515,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;