From 2a5b737a9448462f0006ffce01251090b44a5b3f Mon Sep 17 00:00:00 2001 From: Luca Colonnello Date: Sun, 12 Jan 2020 12:03:11 +0000 Subject: [PATCH] [android][web-browser] Fix WebBrowser sending 'dismiss' before opening Fixes https://github.com/expo/expo/issues/6679 TL;DR A wrong usage of AppState causes WebBrowser to resolve with `{ type: 'dismiss' }` before opening the view. This is related to AppState being not initialised yet, triggering a change before the user leaves the app view to focus on the browser view. --- packages/expo-web-browser/src/WebBrowser.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/expo-web-browser/src/WebBrowser.ts b/packages/expo-web-browser/src/WebBrowser.ts index 37ee736ffcb01..dc19bcc0d6660 100644 --- a/packages/expo-web-browser/src/WebBrowser.ts +++ b/packages/expo-web-browser/src/WebBrowser.ts @@ -149,7 +149,18 @@ let _redirectHandler: ((event: RedirectEvent) => void) | null = null; // returns to active let _onWebBrowserCloseAndroid: null | (() => void) = null; +// Store previous app state to check whether the listener has ever been attached +let _previousAppState: null | string = AppState.currentState; + function _onAppStateChangeAndroid(state: AppStateStatus) { + // if _previousAppState is null, we assume that the first call to + // AppState#change event is not actually triggered by a real change + // (https://facebook.github.io/react-native/docs/appstate#basic-usage) + if (_previousAppState === null) { + _previousAppState = state; + return; + } + if (state === 'active' && _onWebBrowserCloseAndroid) { _onWebBrowserCloseAndroid(); }