coderslay
coderslay

Reputation: 14370

How to close an android application

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

Upvotes: 1

Views: 1463

Answers (8)

Guillermo Zooby
Guillermo Zooby

Reputation: 612

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

Upvotes: 0

Jazz
Jazz

Reputation:

You have to call activity.finish()

Upvotes: 0

JulioStyle88
JulioStyle88

Reputation: 105

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.

Upvotes: 2

Loay
Loay

Reputation: 39

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


    }

Upvotes: 1

LEX
LEX

Reputation: 634

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

Upvotes: -1

Anup Rojekar
Anup Rojekar

Reputation: 1103

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

Upvotes: 0

Vladimir Ivanov
Vladimir Ivanov

Reputation: 43108

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.

Upvotes: 2

Guillaume Brunerie
Guillaume Brunerie

Reputation: 5381

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.

Upvotes: 0

Related Questions