WebViewClient 中的 Android 后退按钮不会返回到当前 Activity
我想让按下按钮显示网页,我使用 WebViewClient 和 WebChromClient 的标准方法,如下所示:
final Button revsButton = (Button) this.findViewById(R.id.buttonReviews);
revsButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
wb.setWebViewClient(new ShareWebViewClient());
wb.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
activity.setTitle("Loading Reviews...");
activity.setProgress(progress * 100);
if(progress == 100)
activity.setTitle(R.string.app_name);
}
});
wb.getSettings().setJavaScriptEnabled(true);
wb.loadUrl(revString );
setContentView(wb);
}
});
private class ShareWebViewClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
}
这一切都很好,问题是当我按下“后退”按钮时,它会完全退出我的 Activity 并返回到上一个 Activity页。我真正想要后退按钮做的是关闭 WebViewClient 并返回到带有按钮的显示。到目前为止,这就是我尝试做的事情,其中一半有效 - 它没有离开活动,但活动页面只是一个空白屏幕,而不是显示应该在那里的控件。
@Override
public void onBackPressed() {
if (wb.getVisibility() == View.VISIBLE) {
wb.setVisibility(View.INVISIBLE);
} else {
finish();
}
}
I want to have a button press display a web page, I use the standard approach with a WebViewClient and WebChromClient like so:
final Button revsButton = (Button) this.findViewById(R.id.buttonReviews);
revsButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
wb.setWebViewClient(new ShareWebViewClient());
wb.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
activity.setTitle("Loading Reviews...");
activity.setProgress(progress * 100);
if(progress == 100)
activity.setTitle(R.string.app_name);
}
});
wb.getSettings().setJavaScriptEnabled(true);
wb.loadUrl(revString );
setContentView(wb);
}
});
private class ShareWebViewClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
}
This is all working great, the thing is when I press the Back button it exits my Activity entirely and returns to the previous page. What I really want the back button to do is to close the WebViewClient and return to the dispaly with the button on it. This is what I tried to do so far which half worked -- it didn't leave the activity, but the activity page was just a blank screen instead of showing the controls that should be on there.
@Override
public void onBackPressed() {
if (wb.getVisibility() == View.VISIBLE) {
wb.setVisibility(View.INVISIBLE);
} else {
finish();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想发生这种情况是因为您使用的是
WebViewClient
而不是使用标准启动浏览器:在这种情况下,“后退”按钮将关闭浏览器活动,您将恢复主要活动。
在你的情况下,我会建议在
onBackPressed()
- 恢复你的原始内容选择I suppose this happens because you're using
WebViewClient
rather than launching browser using standard:In this case BACK button would close browser activity and you'll get back your primary activity.
In your case I would propose in
onBackPressed()
- restore your original content selecting