1

I have a couple of activities in my project which will be called when different buttons will be pressed.

What I'm looking for is a way to close and exit the application completely whenever and wherever the user presses the BACK or Home Button!how should I handle this?

here is my code,but here I navigate this page to the Login Class which is my start up activity!

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(keyCode == KeyEvent.KEYCODE_HOME)
    {
        Intent intent = new Intent(this, Login.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        startActivity(intent);
    }
    else if(keyCode==KeyEvent.KEYCODE_BACK)
    {
        Intent intent = new Intent(this, Login.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        startActivity(intent);
    }
    Toast.LENGTH_LONG).show();
    return false;
}

Any Suggestions will be appreciated.

Rushabh Patel
  • 3,052
  • 4
  • 26
  • 58

2 Answers2

2

you can override onBackpressed() on your mainActivity and call finish() but if you see your login page when you call finish() on your main or when you use backbutton you can set this flag below for your intent to clean your activity stack

 intent = new Intent(this,Activity.class);
 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Arash GM
  • 10,316
  • 6
  • 58
  • 76
  • would you please tell me where should I put this code?because when I write it in the onBackpressed() method it will deactivates this button!Thanks a lot! –  Dec 24 '12 at 07:41
  • 1
    you should use this flag when you are moving from your main or login Activity to next activity not in onBackPressed(). override OnBackPressed with finish() statement to finish your main activity. – Arash GM Dec 24 '12 at 07:45
2

use this code in where ever you use the intent

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

User
  • 1,251
  • 1
  • 14
  • 19