user1831490
user1831490

Reputation:

Exiting an application in Android

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.

Upvotes: 1

Views: 165

Answers (2)

User
User

Reputation: 1271

use this code in where ever you use the intent

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

Upvotes: 2

Arash GM
Arash GM

Reputation: 10395

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);

Upvotes: 2

Related Questions