如何防止webview在旋转时重新加载

时间:2018-02-18 23:31:15

标签: android android-studio webview

我正在尝试实现一个webview,在旋转时维护当前页面和页面数据。我将动态内容加载到客户端,如果屏幕旋转并重新加载页面,则必须重新开始。

我已经跟随其他stackoverflow问题(主要是这一个:Android - Preventing WebView reload on Rotate),但我没有运气。

我覆盖onSaveInstanceStateonRestoreInstanceState保存并恢复web_view,但它对我不起作用。我还检查savedInstanceState是否为null,以及何时加载url。

以下是我项目的文件:

Main.java

package com.mypackage.name;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.CookieManager;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;

import static android.content.Intent.ACTION_VIEW;

/**
 * Created by Admin on 2/15/2018.
 */

public class main extends AppCompatActivity {

    WebView web_view;
    SwipeRefreshLayout swipe_refresh_layout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        web_view = (WebView) findViewById(R.id.web_view);

        swipe_refresh_layout = (SwipeRefreshLayout) findViewById(R.id.swipeContainer);

        CookieManager cookie_manager = CookieManager.getInstance();
        cookie_manager.setAcceptCookie(true);

        WebSettings web_settings = web_view.getSettings();
        web_settings.setJavaScriptEnabled(true);
        web_settings.setAppCacheEnabled(true);
        web_settings.setJavaScriptCanOpenWindowsAutomatically(true);
        web_settings.setSavePassword(false);

        if (savedInstanceState == null) {
            web_view.setWebViewClient(new UriWebViewClient());
            web_view.loadUrl("https://example.com/my_web_site/");
        }
        else {
            web_view.restoreState(savedInstanceState);
        }

        swipe_refresh_layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                web_view.reload();
                swipe_refresh_layout.setRefreshing(false);
            }
        });
        // https://stackoverflow.com/questions/37775213/adding-pull-to-refresh-on-webview-for-refreshing
        // https://stackoverflow.com/questions/12131025/android-preventing-webview-reload-on-rotate
    }

    @Override
    protected void onSaveInstanceState(Bundle outState)
    {
        super.onSaveInstanceState(outState);
        web_view.saveState(outState);
        Log.d("SAVE", "here");
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState)
    {
        super.onRestoreInstanceState(savedInstanceState);
        web_view.restoreState(savedInstanceState);
        Log.d("RESTORE", "here");
    }

    private class UriWebViewClient extends WebViewClient {
        @Override
        public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
            handler.proceed();
        }
    }
}

的AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.package.name">

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >

        <activity android:name=".main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN"
                    android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context=".main"
                android:id="@+id/webview_frame" >

                <WebView
                    android:id="@+id/web_view"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentEnd="true"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentStart="true"
                    android:layout_alignParentTop="true" />

            </FrameLayout>

        </android.support.v4.widget.NestedScrollView>
    </android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

在您的网页浏览设置

中尝试此操作
web_settings.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

您还需要在旋转屏幕时保存Web视图的状态。

@Override  
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);  
m_webview.saveState(outState);

}

然后在调用web_settings

之前重新加载旧的WebView内容
if (savedInstanceState != null) {
m_webview.restoreState(savedInstanceState);
}