gpt4 book ai didi

当按下后退按钮时,android 完成 Activity 包含一个 webview

转载 作者:行者123 更新时间:2023-11-30 03:36:32 24 4
gpt4 key购买 nike

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

这是我的 WebView Activity

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);
}
}


}

最佳答案

覆盖 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);
}

关于当按下后退按钮时,android 完成 Activity 包含一个 webview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16591698/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com