57

Ok. I have looked EVERYWHERE and my little brain just can't understand a better way to refresh an activity. Any suggestions that I can understand would be great. :)

Here is the java code:

package com.dge.dges;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;

public class dgeActivity extends Activity {

    WebView mWebView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings();
        mWebView.loadUrl("http://www.websitehere.php");
        
        Button newButton = (Button)findViewById(R.id.new_button);
        newButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(dgeActivity.this, dgeActivity.class);
                startActivity(intent);
            }
        });
    }
}

And here is the main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000000">

    <WebView
        android:id="@+id/webview"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:scrollbars="none"/>

    <Button
        android:id="@+id/new_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Refresh"/>

</RelativeLayout>

I don't like the idea of just re-stacking activity after activity. There has to be an easier way to refresh the webview. Please help. :)

cdg
  • 583
  • 1
  • 5
  • 5

10 Answers10

94

You could call an mWebView.reload(); That's what it does

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
Josh Hofing
  • 941
  • 1
  • 5
  • 2
64

1) In case you want to reload the same URL:

mWebView.loadUrl("http://www.websitehere.php");

so the full code would be

newButton.setOnClickListener(new View.OnClickListener() {
   public void onClick(View v) {
    dgeActivity.this.mWebView.loadUrl("http://www.websitehere.php");
   }});

2) You can also call mWebView.reload() but be aware this reposts a page if the request was POST, so only works correctly with GET.

Pentium10
  • 204,586
  • 122
  • 423
  • 502
  • Josh answer is the correct one. Your will keep reloading the same url. – Muhammad Babar Mar 18 '15 at 13:53
  • Once after you load a page , you have set the visibility of that button to INVISIBLE like this "newButton.setVisibility(View.INVISIBLE)" – Sudhir Belagali Oct 30 '15 at 12:24
  • 1
    `webView.loadUrl( "javascript:window.location.reload( true )" ); ` this has helped me, webview.reload was not working me, But this needs, `yourWebView.getSettings().setJavaScriptEnabled(true);` enabled in webview Settings – Ramakanth Putta May 28 '18 at 02:21
55

The best solution is to just do webView.loadUrl( "javascript:window.location.reload( true )" );. This should work on all versions and doesn't introduce new history entries.

dragonroot
  • 5,653
  • 3
  • 38
  • 63
  • 6
    Nice solution! However, this will require that you enable JavaScript in your WebView widget with `yourWebView.getSettings().setJavaScriptEnabled(true);` – Markus Rudel Aug 15 '12 at 14:56
  • 1
    I think this should be the best answer. As you wont really need to specify url based on the correct best answer here. – Ryan Jul 13 '17 at 00:48
11

This worked for me.

just put under onCreate metode

Button button = (Button) findViewById(R.id.reload_btn);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            web.loadUrl( "javascript:window.location.reload( true )" );             
        }
    });

This is the code that can reload a website in a webView

web.loadUrl( "javascript:window.location.reload( true )" );  
Thor1401
  • 140
  • 1
  • 12
5

try this :

mWebView.loadUrl(mWebView.getUrl().toString());
Oubaida AlQuraan
  • 1,706
  • 1
  • 18
  • 19
3

Refreshing current webview's URL is not a common usage.
I used this in such a scenario: When user goes to another activity and user come back to webview's activity I reload current URL like this:

public class MyWebviewActivity extends Activity {
    WebView mWebView;
    ....
    ....
    ....
    @Override
    public void onRestart() {
            String url = mWebView.getUrl();
            String postData = MyUtility.getOptionsDataForPOSTURL(mContext);
            mWebView.postUrl(url, EncodingUtils.getBytes(postData, "BASE64"));
    }
}

You can also use WebView's reload() function. But note that if you loaded the webview with postUrl(), then mWebView.reload(); doesn't work. This also works

String webUrl = webView.getUrl();
mWebView.loadUrl(webUrl);
trante
  • 33,518
  • 47
  • 192
  • 272
1

Override onFormResubmission in WebViewClient

@Override
public void onFormResubmission(WebView view, Message dontResend, Message resend){
   resend.sendToTarget();
}
mate00
  • 2,727
  • 5
  • 26
  • 34
  • This worked for me for correctly refreshing a POST request. The other suggested solution of a JS reload adds a history record. Namely each refresh will result in another entry in the back stack. – Niro Jan 17 '22 at 09:51
0

The best and fastest way and should enable JavaScript

WebView webView;

@SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
             webView = findViewById(R.id.webview_sample);
             WebSettings webSettings = webView.getSettings();
             webSettings.setJavaScriptEnabled(true);


webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
            webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
            webView.getSettings().setAppCacheEnabled(true);
            webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
            webSettings.setDomStorageEnabled(true);
         //for Speed up downloading and rendering   
         webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
            webSettings.setUseWideViewPort(true);
            webSettings.setSaveFormData(true);
            webSettings.setEnableSmoothTransition(true);
            webSettings.setAppCacheEnabled(false);

//refresh btn
          Button btn=findViewById(R.id.close_ad_ref);
          btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                 //you can placed in any palce as your need
                 webView.loadUrl( "javascript:window.location.reload( true )" );
            }
        });


}
-4

Yes for some reason WebView.reload() causes a crash if it failed to load before (something to do with the way it handles history). This is the code I use to refresh my webview. I store the current url in self.url

# 1: Pause timeout and page loading

self.timeout.pause()
sleep(1)

# 2: Check for internet connection (Really lazy way)

while self.page().networkAccessManager().networkAccessible() == QNetworkAccessManager.NotAccessible: sleep(2)

# 3:Try again

if self.url == self.page().mainFrame().url():
    self.page().action(QWebPage.Reload)
    self.timeout.resume(60)

else:
    self.page().action(QWebPage.Stop)
    self.page().mainFrame().load(self.url)
    self.timeout.resume(30)

return False
mAsT3RpEE
  • 1,818
  • 1
  • 17
  • 14
-7

Why not to try this?

Swift code to call inside class:

self.mainFrame.reload()

or external call

myWebV.mainFrame.reload()
D.A.H
  • 858
  • 2
  • 9
  • 19