androidwebviewreloadpull-to-refresh

Reload same URL in webview when refresh


I created a little android app to view a responsive website. I have implemented a pull refresh and it works, however when pulling on refresh it redirect to the home page I put in the LoadUrl. How to refresh by reloading the same url the user is visiting ?

My java code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
    swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            LoadWeb();
        }
    });
    LoadWeb();
}
public void LoadWeb(){

    webView = (WebView) findViewById(R.id.webView);
    webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setLoadWithOverviewMode(true);
    webSettings.setAllowFileAccess(true);
    webView.setWebViewClient(new Client());
    webView.setWebChromeClient(new ChromeClient());

    if (Build.VERSION.SDK_INT >= 19) {
        webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
    }
    else if(Build.VERSION.SDK_INT >=15 && Build.VERSION.SDK_INT < 19) {
        webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }
    swipe.setRefreshing(true);
    webView.loadUrl("http://www.example.com"); 
    webView.setWebViewClient(new WebViewClient(){

        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            webView.loadUrl("file:///android_asset/error.html");
        }

        public  void  onPageFinished(WebView view, String url){
            //Hide the SwipeReefreshLayout
            swipe.setRefreshing(false);
        }

    });
}

Main Activity:

<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.example.www.appexample.MainActivity">
<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipe"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<WebView
    android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="0dp" />

</android.support.v4.widget.SwipeRefreshLayout>

Thanks in advance


Solution

  • One way to save the redirect url each time user navigates .

     private String currentUrl="http://www.example.com";
    
    
        webview.setWebViewClient(new WebViewClient()
        {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                currentUrl=url;
                return super.shouldOverrideUrlLoading(view, url);
            }
        });
    

    make your LoadWeb method parameterized to accept a String.

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
            swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    LoadWeb(currentUrl);
                }
            });
            LoadWeb("http://www.example.com");
        }