1

I need to close an application through a button click. How to do this?

user
  • 86,916
  • 18
  • 197
  • 190
coderslay
  • 13,960
  • 31
  • 73
  • 121

8 Answers8

2

It is a very late response, but this is a perfect solution.

Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
startActivity(i); 
android.os.Process.killProcess(android.os.Process.myPid());

Android will take you to the menu and you close the application. easy.

JulioStyle88
  • 105
  • 2
  • 10
2

Basically, android model doesn't consider manual exiting the applications. They are keep in memory as long as resources allow system to do that. But if want force the application to exit you can call finish() on your current activity, but you won't see any difference for your application with clicking back or home button.

Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
1

this work for me

  1. create static Boolean variable, for example (sysExit) on 1st activity = false
  2. override onResume() in every activity
  3. inside onResume add if(sysExit) finish();
  4. on exit button access the Boolean and make it true then finish this activity

this will close all in the stack till application exit ;)

protected void onResume()
    {
        // TODO Auto-generated method stub
        super.onResume();
        if (app_exit)
        {           
            finish();           
            Process.killProcess(android.os.Process.myPid());
        }


    }
Loay
  • 39
  • 3
0

To close Android Application what i do was adding android:launchMode="singleTask" to my main activity in the maniphest...and then this code when i want to close the app

System.runFinalizersOnExit(true);
System.exit(0);
Guillermo Zooby
  • 582
  • 2
  • 15
  • 32
0

You can call the method finish(), this will finish the current activity. Note that this is exactly the same thing as pressing the back button.

Guillaume Brunerie
  • 4,676
  • 3
  • 24
  • 32
0

You need to call finish() method on onClick() method of the of Button.

Anup Rojekar
  • 1,093
  • 9
  • 29
0

You have to call activity.finish()

Parvin Gasimzade
  • 25,180
  • 8
  • 56
  • 83
-1

You can use:

@Override
public void onCreate(Bundle savedInstanceState) 
{ 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.button);
  Button Button1 = (Button) findViewById(R.id.ButtonCloseAbout); 
  Button1.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
  // TODO Auto-generated method stub
  finish();
} 
LEX
  • 634
  • 1
  • 7
  • 11