Reputation: 2254
I'm working on a React Native app that uses a WebView as the app's main screen.
When the app comes from the background to the foreground the WebView reloads. How can I stop this reloading?
This answer suggests loading the WebView in ViewDidLoad()
rather than in ViewDidAppear()
or ViewWillAppear()
. Is this a real solution and how can I confirm, or change, how the WebView is loaded by React Native?
As a hacky solution I'm using onNavigationStateChange
to save the current URL in AsyncStorage
and then load this URL when the app comes to the foreground. This solution isn't great because it:
So I'd much prefer a solution that stops the reloading, at least for situations when the app is only momentarily in the background.
Upvotes: 3
Views: 4039
Reputation: 2254
This was not and issue with the WebView. The reloading was caused by my misuse of the React Navigation SwitchNavigator
. As mentioned in the docs a SwitchNavigator
resets routes to their default state when you switch away
This reset caused the WebView to reload.
Upvotes: 1