无法在WebView中启动新活动

时间:2014-07-04 17:55:17

标签: android webview

我正在尝试在单击webview中的特定链接时启动新活动。但是,它不会创建新活动,而是在Webview中打开链接。

ActivityA.java

myWebView = (WebView) findViewById(R.id.webView3);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.addJavascriptInterface(new WebPageInterface(this),"Android");

ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBarRestaurant);
myWebView.setWebViewClient(new AppWebViewClients(progressBar));

myWebView.loadUrl(webaddress);
myWebView.reload();

public class AppWebViewClients extends WebViewClient {

        private ProgressBar progressBar;

        public AppWebViewClients(ProgressBar progressBar){
            this.progressBar = progressBar;
            progressBar.setVisibility(View.VISIBLE);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.contains("bayview")) {
                Intent intent = new Intent(getBaseContext(), NewAct.class);
                intent.putExtra("url","http://www.stackoverflow.com");
                Log.v("PAGE","Page entered");
                startActivity(intent);
            }
            return true;
        }

       @Override
        public void onPageFinished(WebView view, String url){
            super.onPageFinished(view, url);
            progressBar.setVisibility(View.GONE);
        }
    }

webaddress(在webview中加载网页)

<div data-demo-html="true">
    <ul data-role="listview" data-theme="a" data-split-theme="b" data-split-icon="plus" data-inset="true" id="rest_mesgs">
<li id="0004">
<a href="bayview_abc.html">
<img style="height:80px;" src="BayView.jpg">
<h2 style="text-wrap: normal">Bay View</h2>
<p id="type" class="ui-li-aside"><strong>BB</strong></p>
<p> 123 Road</p>
</a>
</li>
</ul>
</div>

1 个答案:

答案 0 :(得分:0)

这种方法看似神奇:onLoadResource。 这是我做的 - &gt;

 public void onLoadResource(WebView view, String url){
            if(url.contains("bayview")){
                Intent intent = new Intent(getBaseContext(),NewAct.class);
                intent.putExtra("url", "http://www.stackoverflow.com");
                startActivity(intent);
            }
        }

但是,此方法还会使您的webview转到该特定链接。要解决此问题,您可以使用相同的地址onResume再次加载webview。

public void onResume(){
        super.onResume();
        myWebView.loadUrl(webaddress);
    }