2

I am using intents in my app and i have created an exit button. When the exit button is pressed it closes the current activity only and the remaining activities are still executed. This is my code:

b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                finish();
                System.exit(0);

            }

How to exit the entire app when the exit button is pressed? help me to solve to problem.

Prasanth S
  • 3,725
  • 9
  • 43
  • 75
Giridhar94
  • 29
  • 5
  • 4
    Out of curiosity, why would you want to have an exit button in your app? – elimirks Feb 19 '14 at 14:23
  • That's what the home button is for ... – 2Dee Feb 19 '14 at 14:23
  • Please don't put "exit" buttons in an Android app. What possible use is it? Even worse, don't use system.exit(0). It doesn't do what you think it does, it should only be used for very specific reasons, and it's a hack. – Simon Feb 19 '14 at 14:24
  • Having a close button is in general bad design. – Rolf ツ Feb 19 '14 at 14:24
  • Also, TODO Auto-generated method stub, either remove it or use the TODO for something meaningful... – 2Dee Feb 19 '14 at 14:26
  • I had to use an exit button because when the back button is pressed it goes to the previous activity and i have to press the back button several times which moves through all the previous activities and then comes out of the app. So i am trying to have an exit button so that it quits directly the home screen instead of moving through all the previous activities – Giridhar94 Feb 20 '14 at 00:14
  • That's how Android is designed, how it is supposed to work and how users expect it to work. Your app design should change, not Android. Have you looked at the flags that you can use to control the activity navigation stack? – Simon Feb 20 '14 at 08:07

3 Answers3

2

Android doesn't introduce a concept of "close everything I've opened in my app and exit cleanly". The right way of doing so is calling finish() on each of your Activities, this way you're telling to the Android SO you want to exit.

If you have just one Activity, simply calling finish() on it will do the trick. However, if you have many and you handle them putting them in the background/foreground, you may want to read this.

However, don't expect finish() will close your app instantly. Even if you call it, Android will keep it in memory for a while just in case the user wants to open it again in a short amount of time, it has its copy in memory and the loading will be faster, but that's the way.

Community
  • 1
  • 1
nKn
  • 13,691
  • 9
  • 45
  • 62
1

you can try this:

b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent myInt=new Intent (Intent.ACTION_MAIN);
Intent.addCategory(Intent.CATEGORY_HOME);
Intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(myInt);
}});
-2

Try out this one

    @Override
    public void onClick(View arg0) {

        System.exit(0);

    }