简体   繁体   English

带Webview的Android后退按钮

[英]Android Back Button With Webview

I am having an issue with the back button in my android app. 我的Android应用程序中的后退按钮有问题。 I am trying to have it go back through the previous pages and if unable then to exit. 我试图让它回到之前的页面,如果无法再退出。 I pick through the code on the developer website and nothing is working. 我在开发者网站上挑选代码,但没有任何工作。 I would appreciate any help. 我将不胜感激任何帮助。 Below is my activity page: 以下是我的活动页面:

WebView webView; WebView webView;

@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    webView = (WebView)findViewById(R.id.webView);
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webView.setWebViewClient(new myWebView());
    webView.loadUrl("http://google.com");
    webView.setInitialScale(1);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setUseWideViewPort(true);
}

@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) && webView.canGoBack()) {
        webView.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);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.diabetes_meal_planner, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    switch(item.getItemId())
    {
        case R.id.action_settings:
            //exits app
            finish();
            break;

        default:
            break;
    }

    return true;
}

private class myWebView extends WebViewClient
{
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url)
    {
        if(url.startsWith("mailto:"))
        {
            String[] email = url.split(":");
            Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
            emailIntent.setType("plain/text");
            emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{email[1]});
            emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Send_Us_Your_Email");
            Log.v("NOTICE", "Sending Email to" + email[1] + "with subject" + "Send Us Your Feedback");
            startActivity(emailIntent);
        }

        view.loadUrl(url);
        return true;
    }

}

Dont do this onKeyDown . 不要在onKeyDown这样做。 Override Activity.onBackPressed to do this.. 覆盖Activity.onBackPressed来执行此操作..

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webView!=null && webView.canGoBack()) {
        webView.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

Try this at the end, works in my webview app 最后尝试这个,在我的webview应用程序中工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM