[英]Pressing back button exits the app instead of navigating backwards in the WebView
I am working on a WebView android app.我正在开发一个 WebView android 应用程序。 I am unable to fix an issue in my app to back navigate.
我无法修复我的应用程序中的问题以返回导航。 I am using this code and tried all modifications can be made to this.
我正在使用此代码并尝试对此进行所有修改。
public class DeviceActivity extends Activity {
private WebView web;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
web = (WebView) findViewById(R.id.webView);
web.getSettings().setJavaScriptEnabled(true);
web.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
web.getSettings().setPluginsEnabled(false);
web.getSettings().setSupportMultipleWindows(false);
web.getSettings().setSupportZoom(false);
web.setVerticalScrollBarEnabled(false);
web.setHorizontalScrollBarEnabled(false);
// Our application's main page will be loaded
web.loadUrl("file:///android_asset/fbp/index.html");
web.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
public void onBackPressed() {
if (web.canGoBack()) {
web.goBack();
} else {
// My exit alert code goes here.
}
}
});
}
}
This doesn't navigate back but instead, it exits the app.这不会返回,而是退出应用程序。 Any help would be appreciated.
任何帮助,将不胜感激。
Add this in your Activity
code (not in onCreate()
or nested anywhere else)将此添加到您的
Activity
代码中(不在onCreate()
或嵌套在其他任何地方)
@Override
public void onBackPressed() {
if (web.copyBackForwardList().getCurrentIndex() > 0) {
web.goBack();
}
else {
// Your exit alert code, or alternatively line below to finish
super.onBackPressed(); // finishes activity
}
}
This will navigate back through the WebView
history stack until it is empty and then perform the default action (in this case it finishes the activity).这将返回
WebView
历史堆栈,直到它为空,然后执行默认操作(在这种情况下,它完成了活动)。
You should probably also set your WebViewClient to have shouldOverrideUrlLoading
return true
or you won't load any links.您可能还应该将您的 WebViewClient 设置为让
shouldOverrideUrlLoading
返回true
否则您将不会加载任何链接。
In your code you have kept the following block in onCreate() method.在您的代码中,您在 onCreate() 方法中保留了以下块。
if (web.canGoBack()) {
web.goBack();
}
else {
//My exit alert code goes here.
}
Instead keep the same code in onBackPressed() like this而是像这样在 onBackPressed() 中保留相同的代码
@Override
public void onBackPressed() {
if(web.canGoBack()) {
web.goBack();
} else {
super.onBackPressed();
}
}
also refer toGetting Started: WebView-based Applications for Web Developers in Android另请参阅入门:Android 中面向 Web 开发人员的基于 WebView 的应用程序
We can use webview method to navigate forward and backward.我们可以使用 webview 方法来向前和向后导航。 method boolean
canGoForward()
for Gets whether this WebView has a forward history item.方法 boolean
canGoForward()
for 获取此 WebView 是否具有转发历史记录项。 canGoBack()
for Gets whether this WebView has a back history item. canGoBack()
for 获取此 WebView 是否有历史记录项。 goBack()
Goes back in the history of this WebView and goForward()
Goes forward in the history of this WebView. goBack()
在这个 WebView 的历史中回溯, goForward()
在这个 WebView 的历史中前进。 this is my implementation :这是我的实现:
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.forward_btn:
if (mWebView.canGoForward()){
mWebView.goForward();
}else{
Toast.makeText(getApplicationContext(), "Tidak ada halaman lagi!", Toast.LENGTH_SHORT).show();
}
break;
case R.id.backward_btn:
if (mWebView.canGoBack()){
mWebView.goBack();
}else{
Toast.makeText(getApplicationContext(), "Tidak bisa kembali!", Toast.LENGTH_SHORT).show();
}
break;
case R.id.reload_btn:
layoutLoading.setVisibility(View.VISIBLE);
mWebView.reload();
break;
case R.id.back_btn:
onBackPressed();
break;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.