1

I'm trying to implement a webview that on rotate maintains the current page and page data. I have dynamic content being loaded to the client, and they have to start all over if the screen rotates and reloads the page.

I've followed other stackoverflow questions (mostly this one: Android - Preventing WebView reload on Rotate), but I'm not having any luck.

I override onSaveInstanceState and onRestoreInstanceState saving and restoring web_view, but it's not working for me. I also check if savedInstanceState is null or not and when it is I load a url.

Here are the files to my project:

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 Answers1

0

Try this in your webview settings

web_settings.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

also you need to Save the state of the web view when the screen is rotated.

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

}

and then reload the old WebView content before you call for web_settings

if (savedInstanceState != null) {
m_webview.restoreState(savedInstanceState);
}
Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77