My goal:
When a visitor goes back to the first page of my app, a toast appears to let the visitor know that he can press the back button again within 3 seconds to close the Android app.
Specific problem:
When you went back to the first page and the toast appears, the code works fine when you press back only once but when the toast appears and you press back more than one time, the following error message appears: "(appname) has stopped".
Does anyone know what causes this error message and how I can solve this?
MainActivity.java file:
// Open previous opened link from history on webview when back button pressed
private Boolean exit = false;
@Override
// Detect when the back button is pressed
public void onBackPressed() {
if(webView.canGoBack()) {
webView.goBack();
}
else {
if (exit)
this.finish();
else {
Toast.makeText(this, "Press again to exit.",
Toast.LENGTH_SHORT).show();
exit = true;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
exit = false;
}
}, 3 * 1000);
}
}
}
Aucun commentaire:
Enregistrer un commentaire