Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing WebView reload on Rotate - Android Studio

When I rotate my screen, the WebView reloads the whole page. So I disabled rotation in Android_Manifest.xml file, but it would be really cool if I could make rotation possible.

SkateTube.java -

package com.example.skatetube.skatetube;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class SkateTube extends AppCompatActivity {
    private WebView mWebView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_skatetube);


        mWebView = (WebView) findViewById(R.id.activity_main_webview);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        if (savedInstanceState == null)
        {
        mWebView.loadUrl("http://skatetube.sytes.net/");
        mWebView.setWebViewClient(new WebViewClient());

    }



    @Override
    public void onBackPressed() {
        if(mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_skate_tube, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Android_manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.skatetube.skatetube">

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


        <activity
            android:name=".SkateTube"
            android:theme="@style/AppTheme.NoActionBar"

            android:configChanges="keyboard|keyboardHidden|orientation"
            android:label="State preserving implementation"/>



    </application>

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

</manifest>
like image 486
Ronalds Mazītis Avatar asked May 02 '16 16:05

Ronalds Mazītis


2 Answers

All you need to do is Override onSaveInstanceState and onRestoreInstanceState.

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

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState){
    super.onRestoreInstanceState(savedInstanceState);
    mWebView.restoreState(savedInstanceState);
}

and one other solution could be to update your Activity tag in manifest to

android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
like image 193
Max Avatar answered Sep 22 '22 21:09

Max


There are 3 points must be done. This is a full solution for me.

  1. AndroidManifest.xml need config both orientation and screenSize in android:configChanges for target activity

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.skatetube.skatetube">
        <application
            <activity android:name=".WebViewActivity"
                android:configChanges="orientation|screenSize">
            </activity>
        </application>
    </manifest>
    
  2. Need to override onSaveInstanceState and onRestoreInstanceState of Activity to make state can be restore when rotated

    @Override
    protected void onSaveInstanceState(Bundle outState)
    {
        super.onSaveInstanceState(outState);
        mWebView.saveState(outState);
    }
    
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState)
    {
        super.onRestoreInstanceState(savedInstanceState);
        mWebView.restoreState(savedInstanceState);
    }
    
  3. Not loading URL again when screen rotate, use savedInstanceState to know current status in onCreate() of target activity

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        mWebView = (WebView) findViewById(R.id.webView_h5);
        if (savedInstanceState == null) {
            mWebView.loadUrl(url);
        }
    }
    

Miss any one of those three, my activity still reload.

like image 42
Bangyno Avatar answered Sep 21 '22 21:09

Bangyno