2

Possible Duplicate:
android - exit application code

I am finding the way to exit android application to home screen(android phone).

My code is not completely exit application but it turn to the root activity of application.

Here is my code

//Expression
    Button exit = (Button) findViewById(R.id.exit);
    exit.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            // TODO Auto-generated method stub
            finish();
            System.exit(0);
        }
    });

Can you tell me how to exit application and go to home screen of android phone?

Regards,

Community
  • 1
  • 1
SopheakVirak
  • 961
  • 6
  • 14
  • 36
  • For example, if you have 2 activities in stack like A-B, you must finish both of them to exit your app. And `System.exit()` is not required and unnecessary. –  Jul 25 '12 at 05:23
  • Check that all the Activities that you are using are being properly closed..i mean that you should finish all the activites and from your root activity of the App,you then just need to call finish() for that Activity.This will Exit your whole App. – Haresh Chaudhary Jul 25 '12 at 05:29
  • Lai Vung, I tried, but still same. Can you give me some clue? – SopheakVirak Jul 25 '12 at 05:30
  • [Try this one](http://stackoverflow.com/q/11430184/940096) You can probably try accepted answer of this question or `user:LalitPoptani`'s answer. Hope this helps you. – Praveenkumar Jul 25 '12 at 05:44

1 Answers1

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

    /*int pid = android.os.Process.myPid();=====> use this if you want to kill your activity. But its not a good one to do.
    android.os.Process.killProcess(pid);*/

}

call this method where you click exit button

Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78