[英]How to make function back button in actionbar like back button in devices android
我有一個活動,其中有一個webview。 當Web視圖轉到幾頁,並且我單擊了設備上的“后退”按鈕時,它運行正常,但是如果我單擊操作欄中的“后退”按鈕,則會返回錯誤。 如何在操作欄中制作功能后退按鈕,例如設備中的后退按鈕?
這是我的代碼:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tes_backbutton);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
view = (WebView)findViewById(R.id.wv_tes);
progressBar = (ProgressBar)findViewById(R.id.prgbarhome);
view.setWebViewClient(new WebViewClient());
view.loadUrl(URL);
view.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
//Required functionality here
return super.onJsAlert(view, url, message, result);
}
});
view.setWebViewClient(new WebViewClient() {
ProgressDialog progressDialog;
//Show loader on url load
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
if (progressDialog == null) {
// in standard case YourActivity.this
progressDialog = new ProgressDialog(tes_backbutton.this);
progressDialog.setMessage("Loading...");
progressDialog.show();
}
}
public void onPageFinished(WebView view, String url) {
try {
progressDialog.dismiss();
progressDialog = null;
} catch (Exception exception) {
exception.printStackTrace();
}
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Intent myIntent = new Intent(tes_backbutton.this, main_noconnect.class);
tes_backbutton.this.startActivity(myIntent);
}
});
view.canGoBack();
view.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.getAction() == MotionEvent.ACTION_UP
&& view.canGoBack()) {
view.goBack();
return true;
}
return false;
}
});
}
@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
如果您想從操作欄中單擊后退箭頭后執行任何操作,則需要重寫onOptionsItemSelected
方法,如下所示
@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case android.R.id.home:
onBackPressed(); //finishes the activity
return true;
}
return super.onOptionsItemSelected(menuItem);
}
操作欄中的“后退”按鈕應與設備的“后退”按鈕不同。 您不希望他們倆都做同樣的事情。 操作欄中的按鈕應用作“向上”按鈕,而不是“后退”按鈕。 請閱讀有關提供導航的更多信息。
我希望這可以幫助你。
您可以使用onBackPressed(); 方法
@Override public boolean onOptionsItemSelected(MenuItem menuItem){
switch (menuItem.getItemId()) {
case android.R.id.home:
if(mWebView.canGoBack() == true){
mWebView.goBack();
}else{
onBackPressed(); //finishes the activity
}
return true;
}
return super.onOptionsItemSelected(menuItem);
}
you can use webview.cangoback() method for navigating between different web pages in webview
@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case android.R.id.home:
if(view.cangoback()){
view.goback();
}
else{
finish();
}
break;
}
return super.onOptionsItemSelected(menuItem);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.