WebView JavaScript links work when touched, but not when using the trackball

I have a simple WebView-based activity that follows Hello, WebView , to enable JavaScript and reload shouldOverrideUrlLoading().

This operation works great when the user touches links on a web page. However, if the user uses the trackball and clicks on the link, then the resulting page load shows the page with JavaScript disabled.

public class ViewUrl extends Activity {
    protected WebView view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_url);

        final String url = "http://www.opentable.com/phone/reviews.aspx?rid=2947";

        view = (WebView) findViewById(R.id.widget1);
        view.getSettings().setJavaScriptEnabled(true);
        view.loadUrl( url );
        view.setWebViewClient( new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

        });
    }
}

The view_url.xml file is simple:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <WebView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/widget1" />

</RelativeLayout>

Below are screenshots showing both correct and incorrect behavior.

Click "Touch" (CORRECT):

Click using Touch (CORRECT) http://img.skitch.com/20090908-g2acnrpb1riuq11ys3p1u6fd4d.png

Click on the trackball (INCORRECT):

, (INCORRECT) http://img.skitch.com/20090908-my23yxakudwhheq875j2hcwg6h.png

, , , ? , , JS ?

+3
1

, "Hello, WebView" . - IRC ​​ shouldOverrideUrlLoading(), , , .

:

public class ViewUrl extends Activity {
    protected WebView view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_url);

        final String url = "http://www.opentable.com/phone/reviews.aspx?rid=2947";

        view = (WebView) findViewById(R.id.widget1);
        view.getSettings().setJavaScriptEnabled(true);
        view.loadUrl( url );
        view.setWebViewClient( new WebViewClient() ); // probably not necessary if you don't do anything else with the WebViewClient
    }
}
+1

Source: https://habr.com/ru/post/1717193/


All Articles