我的活动中有一个 webview,一旦加载了最终 url,我需要关闭它。我在自定义客户端中尝试过destroy()
,但不允许这样做,finish()
关闭了整个应用程序,返回键也关闭了应用程序。有没有办法只删除 webview 并返回到其余的活动。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
WebView webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new MyWebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(myUrl);
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.matches(PATTERN)) {
Matcher matcher = Pattern.compile(PATTERN).matcher(url);
result = matcher.matches() ? matcher.group(1) : null;
// webview.destroy(); //doesn't work as destroy cannot be called from here
return false;
}
view.loadUrl(url);
return true;
}
}