I have an app in which I have a WebView
where I display some websites. It works, clicking a link in the webpage goes to the next page in the website inside my app. But when I click the phone's back button, it takes me straight into my app. I want to go back to the previous page in the website instead. How can I do this?
我有一个应用,我有一个WebView,可以显示一些网站。它可以工作,点击网页上的一个链接,就会进入到我的app里的下一个页面,但是当我点击手机的后退按钮时,它会直接进入我的app,我想回到网站上的上一个页面。我该怎么做呢?
Here is the code sample I'm using:
下面是我使用的代码示例:
public class Webdisplay extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.webdisplay);
getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
Window.PROGRESS_VISIBILITY_ON);
Toast loadingmess = Toast.makeText(this,
"Cargando El Diario de Hoy", Toast.LENGTH_SHORT);
loadingmess.show();
WebView myWebView;
myWebView = (WebView) findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.elsalvador.com");
myWebView.setWebViewClient(new WebViewClient());
myWebView.setInitialScale(1);
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.getSettings().setUseWideViewPort(true);
final Activity MyActivity = this;
myWebView.setWebChromeClient(new WebChromeClient()
{
public void onProgressChanged(WebView view, int progress)
{
MyActivity.setTitle("Loading...");
MyActivity.setProgress(progress * 100);
if(progress == 100)
MyActivity.setTitle(R.string.app_name);
}
});
}
}
10 个解决方案
#1
411
I use something like this in my activities with WebViews:
我在webview的活动中使用了类似的东西:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (mWebView.canGoBack()) {
mWebView.goBack();
} else {
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
Edit:
编辑:
For this code to work, you need to add a field to the Activity
containing the WebView:
要使此代码工作,您需要向包含WebView的活动添加一个字段:
private WebView mWebView;
Initialize it in the onCreate()
method and you should be good to go.
在onCreate()方法中初始化它,应该很好。
mWebView = (WebView) findViewById(R.id.webView);
#2
237
If using Android 2.2 and above (which is most devices now), the following code will get you what you want.
如果使用Android 2.2或以上版本(现在大多数设备都是如此),下面的代码将为您提供所需的信息。
@Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
#3
83
This is my solution. It works also in Fragment.
这是我的解决方案。它也在片断中起作用。
webView.setOnKeyListener(new OnKeyListener()
{
@Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if(event.getAction() == KeyEvent.ACTION_DOWN)
{
WebView webView = (WebView) v;
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(webView.canGoBack())
{
webView.goBack();
return true;
}
break;
}
}
return false;
}
});
#4
13
Why not use onBackPressed()
?
为什么不使用onBackPressed()?
@Override
public void onBackPressed()
{
// super.onBackPressed(); Do not call me!
// Go to the previous web page.
}
#5
12
Focusing should also be checked in onBackPressed
对焦也应该检查一下
@Override
public void onBackPressed() {
if (mWebview.isFocused() && mWebview.canGoBack()) {
mWebview.goBack();
} else {
super.onBackPressed();
finish();
}
}
#6
9
If you want to go to back page when click on phone's back button, use this:
如果你想在点击手机的后退按钮时回到后退页面,请使用以下方法:
@Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
You can also create custom back button like this:
您也可以创建自定义的后退按钮如下:
btnback.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (wv.canGoBack()) {
wv.goBack();
}
}
});
Full reference for next button and progress bar : put back and next button in webview
完整参考的下一个按钮和进度条:放回和下一个按钮在webview
#7
8
here is a code with confirm exit:
这里有一个带有确认退出的代码:
@Override
public void onBackPressed()
{
if(webView.canGoBack()){
webView.goBack();
}else{
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Exit!")
.setMessage("Are you sure you want to close?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", null)
.show();
}
}
#8
6
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
#9
1
You should the following libraries on your class handle the onBackKeyPressed. canGoBack() checks whether the webview can reference to the previous page. If it is possible then use the goBack() function to reference the previous page (go back).
您的类上的下列库应该处理onBackKeyPressed。canGoBack()检查webview是否可以引用上一个页面。如果可能,那么使用goBack()函数引用前一个页面(返回)。
@Override
public void onBackPressed() {
if( mWebview.canGoBack()){
mWebview.goBack();
}else{
//Do something else. like trigger pop up. Add rate app or see more app
}
}
#10
-2
Webview in Activity,Below code worked for me , Finish the activity the after load the Url in webview.in onbackpressed it go to the previous activity
Webview在活动中,下面的代码为我工作,完成活动后在Webview中加载Url。在onbackpressed中,它返回到先前的活动
webView = (WebView) findViewById(R.id.info_webView);
webView.loadUrl(value);
finish();
#1
411
I use something like this in my activities with WebViews:
我在webview的活动中使用了类似的东西:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (mWebView.canGoBack()) {
mWebView.goBack();
} else {
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
Edit:
编辑:
For this code to work, you need to add a field to the Activity
containing the WebView:
要使此代码工作,您需要向包含WebView的活动添加一个字段:
private WebView mWebView;
Initialize it in the onCreate()
method and you should be good to go.
在onCreate()方法中初始化它,应该很好。
mWebView = (WebView) findViewById(R.id.webView);
#2
237
If using Android 2.2 and above (which is most devices now), the following code will get you what you want.
如果使用Android 2.2或以上版本(现在大多数设备都是如此),下面的代码将为您提供所需的信息。
@Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
#3
83
This is my solution. It works also in Fragment.
这是我的解决方案。它也在片断中起作用。
webView.setOnKeyListener(new OnKeyListener()
{
@Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if(event.getAction() == KeyEvent.ACTION_DOWN)
{
WebView webView = (WebView) v;
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(webView.canGoBack())
{
webView.goBack();
return true;
}
break;
}
}
return false;
}
});
#4
13
Why not use onBackPressed()
?
为什么不使用onBackPressed()?
@Override
public void onBackPressed()
{
// super.onBackPressed(); Do not call me!
// Go to the previous web page.
}
#5
12
Focusing should also be checked in onBackPressed
对焦也应该检查一下
@Override
public void onBackPressed() {
if (mWebview.isFocused() && mWebview.canGoBack()) {
mWebview.goBack();
} else {
super.onBackPressed();
finish();
}
}
#6
9
If you want to go to back page when click on phone's back button, use this:
如果你想在点击手机的后退按钮时回到后退页面,请使用以下方法:
@Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}
You can also create custom back button like this:
您也可以创建自定义的后退按钮如下:
btnback.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (wv.canGoBack()) {
wv.goBack();
}
}
});
Full reference for next button and progress bar : put back and next button in webview
完整参考的下一个按钮和进度条:放回和下一个按钮在webview
#7
8
here is a code with confirm exit:
这里有一个带有确认退出的代码:
@Override
public void onBackPressed()
{
if(webView.canGoBack()){
webView.goBack();
}else{
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Exit!")
.setMessage("Are you sure you want to close?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", null)
.show();
}
}
#8
6
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
#9
1
You should the following libraries on your class handle the onBackKeyPressed. canGoBack() checks whether the webview can reference to the previous page. If it is possible then use the goBack() function to reference the previous page (go back).
您的类上的下列库应该处理onBackKeyPressed。canGoBack()检查webview是否可以引用上一个页面。如果可能,那么使用goBack()函数引用前一个页面(返回)。
@Override
public void onBackPressed() {
if( mWebview.canGoBack()){
mWebview.goBack();
}else{
//Do something else. like trigger pop up. Add rate app or see more app
}
}
#10
-2
Webview in Activity,Below code worked for me , Finish the activity the after load the Url in webview.in onbackpressed it go to the previous activity
Webview在活动中,下面的代码为我工作,完成活动后在Webview中加载Url。在onbackpressed中,它返回到先前的活动
webView = (WebView) findViewById(R.id.info_webView);
webView.loadUrl(value);
finish();