0

I Have developed a android application. In that application I have exit button, I want to exit from my application when I press the EXIT BUTTON. please someone give me a sample code or a tutorial link.

5 Answers5

1

Read (When to Include an Exit Button in Android Apps (Hint: Never)), this article explain you what you need to do and what need not.

Some extract

So, when should your app include an exit button?

The Short Answer

Never.

The Long Answer

Your app doesn't need an exit button. It really doesn't. Arguments to the contrary can generally be summarized as:

  • Users expect it.

  • It's a way to let users say "stop doing everything and stop consuming juice".

Vinayak Bevinakatti
  • 39,623
  • 25
  • 106
  • 137
0

call finish() on the onClick() method of your button

sachy
  • 729
  • 7
  • 13
  • when i call finish() method within app, it exits only my current page. but i want to exit from my application when i press the Exit button. –  May 09 '12 at 09:26
  • You have to call finish() of your main(first) activity i.e. the activity whose category is of `android.intent.category.LAUNCHER` type in manifest file – sachy May 09 '12 at 09:41
0

Set finish() method on your button click event like..

 btn.setOnClickListener(new OnClickListener() {    
        @Override
        public void onClick(View v) {
                    finish();
             }
      });
SBJ
  • 3,999
  • 2
  • 22
  • 27
Jackson Chengalai
  • 3,897
  • 1
  • 23
  • 39
0

Android's design does not favor exiting an application by choice. But rather manages it by the OS, you can bring up the Home application by its corresponding Intent:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

Hop this help!

nnhthuan
  • 1,887
  • 2
  • 17
  • 40
  • 1
    What is this? This way your activity will not be closed, it will stay on the stack. Just call finish(). – Ion Aalbers May 09 '12 at 09:05
  • I think you don't understand. Just make it as home. Otherwise, it it not closed actually. I created hundreds of apps, so this is my experience. :) – nnhthuan May 09 '12 at 09:08
  • Most of people suggest calling finish(), but this is actually not a right way to do. – nnhthuan May 09 '12 at 09:08
  • 1
    I really hope your not serious that you think thats true :-) – Ion Aalbers May 09 '12 at 09:18
  • well. just my suggestion :) you can try yourself. – nnhthuan May 09 '12 at 09:20
  • A few things, 1: what if the user has multiple launchers installed and not selected one as default. 2: Your activity now stays alive, you should only do this when you want it. 3: What if the user were in Application A and then selects this application through the multitask menu. When exiting Application A will also be behind the Launcher. So my suggestion is: Don't mess with the activity stack! – Ion Aalbers May 09 '12 at 09:24
  • We always have many ways to solve a problem based on our needs. Then when I read the question here, I think my code will help him, because I saw many people met this problem. So in your case, if you think my suggestion can't help, you can do it by your own way, in this case is calling `finish()`, and if any ways can solve the problem, so it is the good solution for that problem. So... what I want to say is: nothing the best, nothing can solve every problems. Just try an see if it works. :) Thank you for your attention for 3 cases above. – nnhthuan May 09 '12 at 09:30
-1

If you want close specific activity use finish or if you want to remove stack from Android process use the following code:

    android.os.Process.killProcess(android.os.Process.myPid());
            super.onDestroy();



    protected void onCreate(Bundle savedInstanceState) { 

    this.setContentView(R.layout.layoutxml); 

    this.ExitButton = (Button)this.findViewById(R.id.close); 

    this.ExitButton.setOnClickListener(new OnClickListener() { 

    @Override 

    public void onClick(View v) { 

    finish(); // for stopping Current Activity 

    } 

    });

    // add this line forStoping Entire Application  to Close 
    @Override
        protected void onDestroy() {
    // closing Entire Application
            android.os.Process.killProcess(android.os.Process.myPid());
            super.onDestroy();
        }
    }
Flexo
  • 84,884
  • 22
  • 182
  • 268
sravan
  • 5,165
  • 1
  • 30
  • 33