WebView旋转屏幕后JavaScript失效,重启才恢复,求技术帮助
Hey there! Let's work through this screen rotation issue you're facing with your WebView app—it's a super common pitfall when dealing with dynamic web content like maps and JavaScript.
The Root of the Problem
Your current approach using webView.saveState() and webView.restoreState() sounds logical, but here's the catch: WebView's state-saving methods only preserve basic navigation history and static page elements. They can't fully save or restore dynamic JavaScript contexts, map rendering states, or live web app logic. When the screen rotates, the restored WebView loses those critical dynamic components, which is why your map and JS stop working until you restart.
Solution 1: Prevent Activity Reconstruction (Quick Fix)
The simplest way to avoid this is to tell Android not to destroy and recreate your Activity when the screen rotates. Add this attribute to your Activity in AndroidManifest.xml:
<activity android:name=".YourWebViewActivity" android:configChanges="orientation|screenSize|screenLayout"> </activity>
This keeps your WebView instance intact during rotation, so all JS and map functionality stays alive. Just note: if you need to adjust UI elements for different screen orientations later, you'll have to handle that in the onConfigurationChanged() method.
Solution 2: Reliable State Handling (Recommended for Long-Term)
If you need the Activity to rebuild for orientation-specific UI changes, ditch restoreState() and instead save/restore the current URL. Here's how to adjust your code:
Updated onCreate():
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Initialize WebView settings every time (they don't persist through recreation) webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setGeolocationEnabled(true); webView.getSettings().setAllowFileAccess(true); webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); webView.getSettings().setLoadsImagesAutomatically(true); if (savedInstanceState != null) { // Load the last saved URL instead of restoring state String savedUrl = savedInstanceState.getString("webViewUrl"); if (savedUrl != null) { webView.loadUrl(savedUrl); } else { checkConnection(); // Load your default content } } else { checkConnection(); // Load initial content } }
Updated onSaveInstanceState():
@Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); // Save the current URL instead of WebView state outState.putString("webViewUrl", webView.getUrl()); }
This way, when the Activity rebuilds after rotation, you're reloading the exact page the user was on, which will reinitialize the JavaScript and map properly. It's more reliable than relying on WebView's limited state restoration.
Bonus Tip
If reloading the page feels jarring, you could look into using a Fragment to host the WebView and set setRetainInstance(true) on the Fragment. This keeps the Fragment (and its WebView) alive during Activity recreation, avoiding the need to reload entirely.
内容的提问来源于stack exchange,提问作者Salahuddin Akon

