为什么后退按钮无法使用此代码?

时间:2016-08-03 06:41:45

标签: android

我有一个应用程序,其中我有一个WebView,我在其中显示一些网站。它有效,单击网页中的链接转到我的应用程序内的网站的下一页。但是,当我点击手机的后退按钮时,它会直接进入我的应用程序。我想回到网站的上一页。我怎样才能做到这一点?

这是我的代码。

public class WebView extends AppCompatActivity {

private android.webkit.WebView webView;
private ProgressBar progressBar;
private LinearLayout layoutProgress;


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.webview);

    webView = (android.webkit.WebView) findViewById(R.id.webViewMain);
    progressBar = (ProgressBar) findViewById(R.id.progressBar);
    layoutProgress = (LinearLayout) findViewById(R.id.layoutProgress);
    webView.setVisibility(View.GONE);
    WebSettings settings = webView.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setBuiltInZoomControls(true);
    settings.setSupportZoom(true);
    settings.setDisplayZoomControls(false);

    webView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(android.webkit.WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageFinished(android.webkit.WebView view, String url) {
            webView.setVisibility(View.VISIBLE);
            layoutProgress.setVisibility(View.GONE);
            progressBar.setIndeterminate(false);
            super.onPageFinished(view, url);
        }

        @Override
        public void onPageStarted(android.webkit.WebView view, String url, Bitmap favicon) {
            layoutProgress.setVisibility(View.VISIBLE);
            progressBar.setIndeterminate(true);
            super.onPageStarted(view, url, favicon);
        }
    });
    if (isOnline()) {
        webView.loadUrl("http://www.**********.com/ ");
    } else {
        String summary = "<html><body><font color='red'>No Internet Connection</font></body></html>";
        webView.loadData(summary, "text/html", null);
        toast("No Internet Connection.");
    }
}

private void toast(String message) {
    Toast.makeText(WebView.this, message, Toast.LENGTH_LONG).show();
}

private boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    return (netInfo != null && netInfo.isConnected());
}

@Override
public void onBackPressed() {
    if(webView.canGoBack() == true) {
        webView.goBack();
    } else {
        WebView.super.onBackPressed();
    }
}
}

2 个答案:

答案 0 :(得分:1)

尝试添加keydown

 @Override
 public void onBackPressed() {

    if (webview.canGoBack()) {
        webview.goBack();
    } else {
        finish();
    }


 }

 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        //Display confirmation here, finish() activity.
        if (webview.canGoBack()) {
            webview.goBack();
        } else {
            finish();
        }
        return true;
    }
    return super.onKeyDown(keyCode, event);
  }

答案 1 :(得分:1)

检查此代码,这有助于您

@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();
        /*Intent intent = new Intent(HomeActivity.this, SelectionActivity.class);
        startActivity(intent);
        finish();*/
    }
}