Android: webview
I am trying to wrap a web application say www.xyz.com within a webview using shouldOverrideUrlLoading. I have two activity one that launches the application, this checks all the activity running and determines whether to launch new instance or not and other activity which wraps my application www.xyz.com in the webview.
I'm facing two problems:
When I press the sleep button and then again press it to open the device, or when the application is idle and went to sleep and I press the button to open the device, the application restarts (the webview restarts).
Also when link in the webapplication which opens someother site say for example www.abc.com doesn't work fine, it works for the first time and opens the site in external browser as desired, however on further click tries to open the site within the webview which is not desired.
I have googled this and found suggestion like the code below:
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contai开发者_运维问答ns("abc")) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
return true;
} else {
view.loadUrl(url);
return false;
}
}
However, in my case for the first time the site www.abc.com is opened in external browser but for the second time its open within the webview, i've debugged it in eclipse and found the url value comes the same in subsequent cliks .
1) For the WebView restart problem, you need to add the following lines of code to your activity that contains the WebView
android:launchMode="singleInstance"
android:alwaysRetainTaskState="true"
Also see tutorials on how to save state of WebView in Bundle.
2) For your external browser problem, try
url.equals("www.abc.com")
instead of
url.contains("abc")
Hope that solves your problem :)
精彩评论