Reputation: 141
Hello Guys i'm trying to make the back button on a webview go back when pressed one time and finish the activity with an alert when pressed twice
the problem is that when I try to double click it runs as if I had clicked one .. he does not expect the run time of double click
thanks!
Code
public void onBackPressed()
{
if (back_pressed + 1000 > System.currentTimeMillis()){
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("Esmalteria Cariúcha")
.setMessage("Sair do Sistema?")
.setPositiveButton("Sim", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("Não", null)
.show();
}
else{
if(webview.canGoBack()){
webview.goBack();
}
}
back_pressed = System.currentTimeMillis();
}
Upvotes: 0
Views: 3468
Reputation: 1
Try this
private WebView view;
private String urlhome = "http://localhost/Default.aspx";
private static String currenturl = "";
private Boolean close_app = false;
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (view.canGoBack()) {
String url = view.getUrl();
if (url.equalsIgnoreCase(urlhome)) {
if (close_app) {
// pressed twice
finish();
} else {
Toast.makeText(this, "Press Back again to Exit.",
Toast.LENGTH_SHORT).show();
close_app = true;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
close_app = false;
}
}, 3 * 1000);
return true;
}
} else {
view.goBack(); //method goback()
return true;
}
} else {
String url = view.getUrl();
if (url.equalsIgnoreCase(urlhome)) {
if (close_app) {
// pressed twice
finish();
} else {
Toast.makeText(this, "Press Back again to Exit.",
Toast.LENGTH_SHORT).show();
close_app = true;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
close_app = false;
}
}, 3 * 1000);
return true;
}
}else {
view.loadUrl(urlhome);
currenturl = urlhome;
return true;
}
}
}
return super.onKeyDown(keyCode, event);
}
Upvotes: 0
Reputation: 5097
Try to make this, see if this works for you,
boolean doubleBackToExitPressedOnce;
@Override
public void onBackPressed() {
Log.i(TAG, "onBackPressed");
if (doubleBackToExitPressedOnce) {
Log.i(TAG, "double click");
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("Esmalteria Cariúcha")
.setMessage("Sair do Sistema?")
.setPositiveButton("Sim",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
finish();
}
}).setNegativeButton("Não", null).show();
return;
} else {
Log.i(TAG, "single click");
if (webview.canGoBack()) {
Log.i(TAG, "canGoBack");
webview.goBack();
} else {
Log.i(TAG, "nothing to canGoBack");
}
}
this.doubleBackToExitPressedOnce = true;
if (getApplicationContext() == null) {
return;
} else {
Toast.makeText(this, "Please click BACK again to exit",
Toast.LENGTH_SHORT).show();
}
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
doubleBackToExitPressedOnce = false;
}
}, 2000);
}
Upvotes: 4