0

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

    }
Empi
  • 153
  • 2
  • 18
  • Back button will do that automatically.. – Subburaj Dec 31 '13 at 13:37
  • back button minimalize not close app – Empi Dec 31 '13 at 13:38
  • 1
    @Empi while System.exit() works, `finish()` is the correct way to do this in Android. "Minimized" is enough; Android will kill it off if it needs to reclaim its memory. – mah Dec 31 '13 at 13:41
  • 3
    There is no such thing as "minmised" in Android. This is not a desktop OS. An app is in the foreground (active) or background (inactive). Home or back put the app in the background. It is up to Android whether the app is "exited" or not. You should not try to exit the app yourself (unless you need to stop background services). If you do, you will interfere with the way that Dalvik manages memory and app performance. It is a bad idea. Unless you need to stop something in the background, there should be no "exit" button. They are just annoying to users. – Simon Dec 31 '13 at 13:47
  • thx for explain Simon – Empi Dec 31 '13 at 13:57
  • http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon/2034238#2034238 – CommonsWare Dec 31 '13 at 14:48

4 Answers4

1

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

Mr.Rao
  • 321
  • 2
  • 6
1

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?

Community
  • 1
  • 1
RED_
  • 2,997
  • 4
  • 40
  • 59
0

Try with this single line code :

System.exit(0);
Kamlesh Arya
  • 4,864
  • 3
  • 21
  • 28
  • Great, give the OP the WRONG way to deal with the question and add to the tally of apps and developers who believe that their way is better than how the core OS is designed and have a negative impact on system performance. How many time does it need to be said that you no more terminate an Android app than you do a web page. Please show me the exit button on www.google.com. This thinking belongs on desktop OSes, 5 years ago. System.exit(0) does not even kill the app - I bet you have no idea what it does.-1 – Simon Dec 31 '13 at 17:07
  • @Simon.if u think that my solution was not the efficient one.than my dear friend.why not u just come up with a better solution inspite of blaming someone.! – Kamlesh Arya Jan 02 '14 at 04:21
  • I've already given the correct answer in my comment. Several other people have too. The correct answer is to do nothing and to let Android deal with whether an app is kept in the background or is destroyed. If the OP really wants to, then use `finish()` on an Activity but this is more to do with the navigation stack than the OPs question. – Simon Jan 02 '14 at 16:09
0

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.

Ilya Gazman
  • 31,250
  • 24
  • 137
  • 216