0

When I rotate my screen, the WebView reloads the whole page. I can't have this since some of my content contains dynamic/random material. Currently, when rotated the screen reloads the original URL from the loadUrl() method.

Any idea what's wrong with my code?

AndroidManifest.xml

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

<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=".MainActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
</application>

</manifest>

MainActivity.java

package com.myworldrules.apps.lifehacks;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import  android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String url = "http://hacks.myworldrules.com";
    WebView view=(WebView) this.findViewById(R.id.webView);
    view.getSettings().setJavaScriptEnabled(true);
    view.loadUrl(url);

    view.setWebViewClient(new WebViewClient());



}



}

Activity.main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.myworldrules.apps.lifehacks.MainActivity">

<WebView
    android:id="@+id/webView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />


</android.support.constraint.ConstraintLayout>

Can you guys please modify my code, and please fix my code.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 3
    Please refer this thread - [Android - Preventing WebView reload on Rotate](https://stackoverflow.com/questions/12131025/android-preventing-webview-reload-on-rotate) – Sachin Aggarwal Aug 22 '17 at 20:05
  • 1
    Possible duplicate of [Android - Preventing WebView reload on Rotate](https://stackoverflow.com/questions/12131025/android-preventing-webview-reload-on-rotate) – Toastrackenigma Aug 22 '17 at 21:06

1 Answers1

0

You can make changes in your manifest file as follows -

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

    <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=".MainActivity"
              android:configChanges="orientation|screenSize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    </application>

    </manifest>
Sachin Aggarwal
  • 1,095
  • 8
  • 17
  • Thanks for the help on this. Can you help me on this question: https://stackoverflow.com/questions/45827508/webview-not-updating-when-i-change-the-website –  Aug 23 '17 at 01:25