1

我有一个 android 应用程序,其中有一个按钮。用户按下该按钮,然后它启动一个包含 webview 的活动。问题是为了在用户第一次按下后退按钮时完成 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);
    }
}


}
4

1 回答 1

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);
}
于 2013-05-16T15:43:37.593 回答