I need to quit my application, I have referred all the links in Stack Overflow and also on other sites, I have used finishAffinity()
, finish()
, System.exit(0)
still I am not able to achieve it. I am overriding onBackPressed method in Main Activity and terminating the application.
Actually it works fine when I press back button after launching the application. But when I move on to other activities and come back to MainActivity, it is not working. If I use finishAffinity()
it is opening my Camera Activity which is one of my Activity in the project. If I use finish()
it is opening my second page activity.
I will post my code for reference.
MainActivity.Java
@Override
public void onBackPressed()
{
if (back_pressed + 2000 > System.currentTimeMillis())
{
Log.e("called", "back pressed");
finish();
}
else {
Toast.makeText(getBaseContext(), "Press twice to exit!", Toast.LENGTH_SHORT).show();
}
back_pressed = System.currentTimeMillis();
}