Empi
Empi

Reputation: 163

Exit from Application android

I tried some way to exit from application but each method just minimizes the app. I'd like to close down the app not minimize it. Exit Button is in MainAcitivity.

For now i'm calling this function, it works but not as i need.

public void AppExit()
{
    this.finish();
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}

I know this question is asked a lot but i can't find solution exactly what i need.

After your advice i use this function

public void AppExit()
    {
        System.exit(0);
        finish();

    }

Upvotes: 0

Views: 449

Answers (4)

RED_
RED_

Reputation: 3007

Few things. In your code you can put finish(); after startActivity, this may help (did for me).

How it should be done however is the way the guidelines have it already. The back button does close the app. If you press the back button and then restart your app, you will be on the very first page activity, not where you left off.

In regards to the back button, you can Override it to control what it does if you are not happy with it. You can put your code in there and when the users presses the back button it will execute the code. Users are more accustomed to pressing the back button rather than a button within a layout (which I presume is what you may be doing).

This might be helpful if you are sticking to what you have: How to clear the Android Stack of activities?

Upvotes: 1

Mr.Rao
Mr.Rao

Reputation: 331

If you want to exit why you are starting activity again. use only this.finish(), remove other statements. Take look at activity life cycle for more info

Upvotes: 1

Ilya Gazman
Ilya Gazman

Reputation: 32271

You need to navigate to your main activity with Intent.FLAG_ACTIVITY_CLEAR_TOP, pass it some data that it will know that you are exiting the application. And just call finish from your main activity.

Another suggestion is Kamlesh Arya answer. How ever it is a bad practice as it will not destroy the activity stack, and next time the use lunch your application it will not start from the main activity but from the last active activity.

Upvotes: 0

Kamlesh Meghwal
Kamlesh Meghwal

Reputation: 4972

Try with this single line code :

System.exit(0);

Upvotes: 0

Related Questions