Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dead loop in android webview backkey for redirect href link

I got to a dead loop in Android webview application with backkey while dealing with redirect links.

For example, when my webview started, it goes to link0.

In link0, there is a href link which link to link1. Link1 redrect to link2.

So if I click link1, it will go to link1, then redirect to link2. When I click backkey, it should go back to link0, in my case. But instead, it goes to link1, which redirect back to link2 again. So I never have a chance to go back.

The backkey works correctly with other links if they are not redirect links.

I googled webs for help, but didn't find related question.

By the way, the backkey works in the internet browser as expected. But not in webview.

Below is my code, for you to try. As you can see in the code, I tried both onBackPressed and onKeyDown, but neigher works.

Thanks for your kind help. I have struglling on this for a while.

==================================================================================

public class MyActivity extends Activity 
{
    private WebView myWebView;

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

        myWebView = (WebView) findViewById(R.id.webview);
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.loadUrl("http://50.112.242.120/temp/");
        myWebView.setWebViewClient(new MyWebViewClient());
    }

    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            myWebView.loadUrl(url);
            return true;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // Check if the key event was the Back button and if there's history
        if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
            myWebView.goBack();
            return true;
        }
        // If it wasn't the Back key or there's no web page history, bubble up to the default
        // system behavior (probably exit the activity)
        return super.onKeyDown(keyCode, event);
    }
}

.

// main.xml
<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>
like image 505
Blue Angle Avatar asked Aug 16 '12 17:08

Blue Angle


People also ask

How do I know if a URL is loaded in WebView?

myWebView. setWebViewClient(new MyWebViewClient()); public class MyWebViewClient extends WebViewClient { ... @Override public void onPageFinished(WebView view, String url) { // Make a note that the page has finished loading. } ... }

How do you override a WebView?

If you want to override certain methods, you have to create a custom WebView class which extends WebView . Also, when you are inflating the WebView , make sure you are casting it to the correct type which is CustomWebView . CustomWebView webView = (CustomWebView) findViewById(R. id.

How do I enable JavaScript on Android WebView?

Enable JavaScript JavaScript is disabled in a WebView by default. You can enable it through the WebSettings attached to your WebView . You can retrieve WebSettings with getSettings() , then enable JavaScript with setJavaScriptEnabled() . WebView myWebView = (WebView) findViewById(R.


3 Answers

webview.setWebViewClient(new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Log.i(tag, "url = " + url);
            // view.loadUrl(url);

            // return super.shouldOverrideUrlLoading(view, url);
            return false;
        }
    });

look!!!! the important, shouldOverrideUrlLoading must return false

in shouldOverrideUrlLoading you can't invoke view.loadUrl.

like image 96
lingyfh Avatar answered Oct 29 '22 22:10

lingyfh


shouldOverrideUrlLoading() is called on a per-URL basis. So, if http://site1.com/ redirects to http://site2.com/, which then redirects to http://site3.com/, you are calling WebView.loadUrl() for each of these URL's. Thus each appears in the back stack.

Instead of creating a WebViewClient you probably want a WebChromeClient. WebChromeClient defines a method onCreateWindow that is only invoked when the WebView is trying to create a new window, not for every single URL it accesses.

like image 41
j__m Avatar answered Oct 30 '22 00:10

j__m


I had a same problem and solved it. Here is my answer.

When I click the first link(www.new.a) it automatically redirects other link(mobile.new.a). Usually the links redirect two or three, and my solution have been worked on almost every redirect links. I hope this answer help you out with annyoing redirecting links.

I finally figured out that. You need a WebViewClient with four APIs. There are shouldOverrideUrlLoading(), onPageStarted(), onPageFinished(), and doUpdateVisitedHistory() in the WebViewClient. All the APIs you need is API 1 so don't worry about.

Here is my answer. Check out that! :)

like image 1
theWook Avatar answered Oct 30 '22 00:10

theWook