You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WebView旋转屏幕后JavaScript失效,重启才恢复,求技术帮助

Fixing WebView Map & JavaScript Failure After Screen Rotation

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.

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 14:52:46