Is there a better way to refresh WebView?

2020-01-23 16:53发布

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. :)

8条回答
放荡不羁爱自由
2楼-- · 2020-01-23 17:30

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.

查看更多
趁早两清
3楼-- · 2020-01-23 17:32

try this :

mWebView.loadUrl(mWebView.getUrl().toString());
查看更多
Bombasti
4楼-- · 2020-01-23 17:33

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

查看更多
贪生不怕死
5楼-- · 2020-01-23 17:33

Why not to try this?

Swift code to call inside class:

self.mainFrame.reload()

or external call

myWebV.mainFrame.reload()
查看更多
够拽才男人
6楼-- · 2020-01-23 17:36

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 )" );  
查看更多
爷、活的狠高调
7楼-- · 2020-01-23 17:36

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);
查看更多
登录 后发表回答