I'm developing an Android Webview app. I would like to use onbackpressed
to go to the previous page but also to exit the app.
How it should work:
- Pressing back-button on homepage: I want to show a message "Press once again to exit."
- Pressing back-button on other webpages: it should immediately open the previous page.
I tried to achieve this by using the following code. The only problem is that I don't know how to set my homepage as a variable. Right now I'm getting the error message "Page cannot be resolved as a variable". I understand why I get that message, I just don't know how to solve it in this particular case.
Let's say my homepage url is "http://example.com", how do I set this homepage as a variable?
private Boolean exit = false;
@Override
public void onBackPressed() {
if(page != "homepage"){
super.onBackPressed(); // Calls the Overriden Method
}
else
{
if (exit)
this.finish();
else {
Toast.makeText(this, "Press once again to exit.",
Toast.LENGTH_SHORT).show();
exit = true;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
exit = false;
}
}, 3 * 1000);
}
}
}
maybe this will help: