From 3686e3665557603b10e2793d89748239a2b96075 Mon Sep 17 00:00:00 2001 From: Luca Colonnello Date: Fri, 17 Apr 2020 00:12:05 +0100 Subject: [PATCH] [android][web-browser] Fix WebBrowser sending 'dismiss' before opening (#6743) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [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. * Build packages * Updated CHANGELOG * Store only if AppState is available instead of every state change * Clean yarn.lock * Update packages/expo-web-browser/CHANGELOG.md Co-Authored-By: Łukasz Kosmaty Co-authored-by: Łukasz Kosmaty Co-authored-by: Brent Vatne --- packages/expo-web-browser/CHANGELOG.md | 2 ++ packages/expo-web-browser/build/WebBrowser.js | 9 +++++++++ packages/expo-web-browser/src/WebBrowser.ts | 10 ++++++++++ 3 files changed, 21 insertions(+) diff --git a/packages/expo-web-browser/CHANGELOG.md b/packages/expo-web-browser/CHANGELOG.md index 7544efc39da36..ab076e05ee176 100644 --- a/packages/expo-web-browser/CHANGELOG.md +++ b/packages/expo-web-browser/CHANGELOG.md @@ -9,3 +9,5 @@ - Add `readerMode` and `dismissButtonStyle` (iOS) and `enableDefaultShare` (Android) flags for `WebBrowser` ([#7221](https://github.com/expo/expo/pull/7221) by [@LinusU](https://github.com/LinusU)) & [@mczernek](https://github.com/mczernek)) ### 🐛 Bug fixes + +- Fix `WebBrowser` sending `dismiss` before opening. ([#6743](https://github.com/expo/expo/pull/6743) by [@LucaColonnello](https://github.com/LucaColonnello)) diff --git a/packages/expo-web-browser/build/WebBrowser.js b/packages/expo-web-browser/build/WebBrowser.js index 3174d1ceb1290..8959bca90188a 100644 --- a/packages/expo-web-browser/build/WebBrowser.js +++ b/packages/expo-web-browser/build/WebBrowser.js @@ -120,7 +120,16 @@ let _redirectHandler = null; // Store the `resolve` function from a Promise to fire when the AppState // returns to active let _onWebBrowserCloseAndroid = null; +// If the initial AppState.currentState is null, we assume that the first call to +// AppState#change event is not actually triggered by a real change, +// is triggered instead by the bridge capturing the current state +// (https://facebook.github.io/react-native/docs/appstate#basic-usage) +let _isAppStateAvailable = AppState.currentState !== null; function _onAppStateChangeAndroid(state) { + if (!_isAppStateAvailable) { + _isAppStateAvailable = true; + return; + } if (state === 'active' && _onWebBrowserCloseAndroid) { _onWebBrowserCloseAndroid(); } diff --git a/packages/expo-web-browser/src/WebBrowser.ts b/packages/expo-web-browser/src/WebBrowser.ts index fa21422a7df5b..7de6ecc51866f 100644 --- a/packages/expo-web-browser/src/WebBrowser.ts +++ b/packages/expo-web-browser/src/WebBrowser.ts @@ -165,7 +165,17 @@ let _redirectHandler: ((event: RedirectEvent) => void) | null = null; // returns to active let _onWebBrowserCloseAndroid: null | (() => void) = null; +// If the initial AppState.currentState is null, we assume that the first call to +// AppState#change event is not actually triggered by a real change, +// is triggered instead by the bridge capturing the current state +// (https://facebook.github.io/react-native/docs/appstate#basic-usage) +let _isAppStateAvailable: boolean = AppState.currentState !== null; function _onAppStateChangeAndroid(state: AppStateStatus) { + if (!_isAppStateAvailable) { + _isAppStateAvailable = true; + return; + } + if (state === 'active' && _onWebBrowserCloseAndroid) { _onWebBrowserCloseAndroid(); }