user8475346
user8475346

Reputation:

WebView not updating when I change the website

So I have my website linked on to my web view. However, when I update my website and restart my app, the changes do not reflect on the app. Here is my code:

Here is my code:

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 {
private WebView view;
@Override
protected void onCreate(Bundle savedInstanceState) {

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

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

    view.setWebViewClient(new WebViewClient());

}

}

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"
        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>

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>

Please help me with my code.

Upvotes: 1

Views: 3306

Answers (1)

Sachin Aggarwal
Sachin Aggarwal

Reputation: 1135

Modify your java code as -

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 {
private WebView view;
@Override
protected void onCreate(Bundle savedInstanceState) {

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

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


    view.setWebViewClient(new WebViewClient());

}

}

Upvotes: 3

Related Questions