Handling web browser closing on Android

Hi everyone.

According to this doc, openBrowserAsync promise does not notify if the browser has been closed.
Is there any chance to find out about it from the component that called WebBrowser.openBrowserAsync(url)?

I tried adding a listener on ‘focus’ event but it didn’t work, apparently because the screen was still focused despite it was covered by WebBrowser.

Hello! Did you find solution how to make it on android !?

Hi Alex!
Kind of. There’s a workaround I use.

When I open the browser, I set a variable.

const [openedBrowser, setOpenedBrowser] = useState(false);
// ...
let result = await WebBrowser.openBrowserAsync(url, { enableBarCollapsing: true });
setOpenedBrowser(true);

Then I hook on the variable value that enables a timer that checks if the browser has been closed. This timer won’t work in the background, so the next tick is going to happen as soon as the browser has closed.

useEffect(() => {
    if (openedBrowser) {
        const observer = setInterval(() => {
            // your logic: anything you need to happen when the browser has closed
            setOpenedBrowser(false);
        }, 500);
        return () => {
            clearInterval(observer);
        };
    }
}, [openedBrowser]);

more precisely, we can listen to AppState (AppState · React Native) because when the browser open, the app is in background. So, when the app state change to active, the browser has been closed and back to the app.