按下后退按钮时,android完成活动包含webview

时间:2013-05-16 15:33:41

标签: android android-webview back-button

我有一个Android应用程序,其中我有一个按钮。用户按下该按钮然后它启动包含webview的活动。 问题是为了在用户第一次按回按钮时完成webview活动,我试图完成那个活动但是没有工作,它回到历史页面。我按了几次按钮,webview活动完成。 当用户第一次按下按钮时,我的应用如何完成webview活动?

这是我的网络视图活动

public class SocialWebViewActivity extends Activity {

private WebView webView;
ProgressDialog mProgress;
public static String url="";
@SuppressLint("SetJavaScriptEnabled")
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.social_web_view);
    webView = (WebView) findViewById(R.id.social_web_view_external);
    webView.getSettings().setJavaScriptEnabled(true);

     // the init state of progress dialog
    mProgress = ProgressDialog.show(this, "Web", "Loading...");

    // add a WebViewClient for WebView, which actually handles loading data from web
    webView.setWebViewClient(new WebViewClient() {
        // load url
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        // when finish loading page
        public void onPageFinished(WebView view, String url) {
            if(mProgress.isShowing()) {
                mProgress.dismiss();
            }
            view.clearHistory();
        }

        @Override
        public void onReceivedError(WebView view, int errorCode,
                String description, String failingUrl) {
            Toast.makeText(getApplicationContext(), description, Toast.LENGTH_LONG).show();
            if(mProgress.isShowing()) {
                mProgress.dismiss();
            }
            super.onReceivedError(view, errorCode, description, failingUrl);
        }
    });


    if(!"".equals(SocialWebViewActivity.url)){
        webView.loadUrl(SocialWebViewActivity.url);
    }
}


}

1 个答案:

答案 0 :(得分:1)

覆盖onKeyDown(param)

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) ) {
        webView.clearHistory(); // clear history
        finish(); // finish activity
        return true;
    }

    return super.onKeyDown(keyCode, event);
}