Browse Source

Send initial screen appear event after it's displayed

To prevent race condition happening when the event was sent before the screen was mounted
Guy Carmeli 7 years ago
parent
commit
44962b7eb1

+ 25
- 3
android/app/src/main/java/com/reactnativenavigation/screens/ScreenStack.java View File

@@ -417,11 +417,33 @@ public class ScreenStack {
417 417
         isStackVisible = true;
418 418
         stack.peek().setStyle();
419 419
         stack.peek().setVisibility(View.VISIBLE);
420
-        stack.peek().getScreenParams().timestamp = System.currentTimeMillis();
421
-        NavigationApplication.instance.getEventEmitter().sendWillAppearEvent(stack.peek().getScreenParams(), type);
422
-        NavigationApplication.instance.getEventEmitter().sendDidAppearEvent(stack.peek().getScreenParams(), type);
420
+        sendScreenAppearEvent(type, stack.peek());
423 421
     }
424 422
 
423
+    private void sendScreenAppearEvent(final NavigationType type, final Screen screen) {
424
+        if (type == NavigationType.InitialScreen) {
425
+            sendInitialScreenAppearEvent(type, screen);
426
+        } else {
427
+            sendScreenAppearEvent(screen, type);
428
+        }
429
+    }
430
+
431
+    private void sendInitialScreenAppearEvent(final NavigationType type, final Screen screen) {
432
+        screen.setOnDisplayListener(new Screen.OnDisplayListener() {
433
+            @Override
434
+            public void onDisplay() {
435
+                sendScreenAppearEvent(screen, type);
436
+            }
437
+        });
438
+    }
439
+
440
+    private void sendScreenAppearEvent(Screen screen, NavigationType type) {
441
+        screen.getScreenParams().timestamp = System.currentTimeMillis();
442
+        NavigationApplication.instance.getEventEmitter().sendWillAppearEvent(screen.getScreenParams(), type);
443
+        NavigationApplication.instance.getEventEmitter().sendDidAppearEvent(screen.getScreenParams(), type);
444
+    }
445
+
446
+
425 447
     public void hide(NavigationType type) {
426 448
         NavigationApplication.instance.getEventEmitter().sendWillDisappearEvent(stack.peek().getScreenParams(), type);
427 449
         NavigationApplication.instance.getEventEmitter().sendDidDisappearEvent(stack.peek().getScreenParams(), type);