Q:

How to kill an application with all its activities?

Possible Duplicate:
Quitting an application - is that frowned upon? 

I want to offer the user an option to exit the application as I need to delete some sensitive data, which is stored in the SharedPreferences as long as the application needs it.
As soon as the user wants to exit, the password in the SharedPreferences should be wiped and of course all activities of the application should be closed (it makes no sense to run them without the known password - they would crash).
How can I do that?
System.exit(0) and finish() only exit the current activity - useless. I know there is a taskmanager app. How is that one doing it? It's able to kill the whole application...

A:

When you use the finish() method, it does not close the process completely , it is STILL working in background. 
Please use this code in Main Activity (Please don't use in every activities or sub Activities):
@Override
public void onBackPressed() {

    android.os.Process.killProcess(android.os.Process.myPid());
    // This above line close correctly
}

A:

You are correct: calling finish() will only exit the current activity, not the entire application. however, there is a workaround for this: 
Every time you start an Activity, start it using startActivityForResult(...). When you want to close the entire app, you can do something like this:
setResult(RESULT_CLOSE_ALL);
finish();

Then define every activity's onActivityResult(...) callback so when an activity returns with the RESULT_CLOSE_ALL value, it also calls finish():
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch(resultCode)
    {
    case RESULT_CLOSE_ALL:
        setResult(RESULT_CLOSE_ALL);
        finish();
    }
    super.onActivityResult(requestCode, resultCode, data);
}

This will cause a cascade effect closing all activities.
Also, I support CommonsWare in his suggestion: store the password in a variable so that it will be destroyed when the application is closed.

A:

When the user wishes to exit all open activities, they should press a button which loads the first Activity that runs when your app starts, in my case "LoginActivity".  
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);

The above code clears all the activities except for LoginActivity.  LoginActivity is the first activity that is brought up when the user runs the program.  Then put this code inside the LoginActivity's onCreate, to signal when it should self destruct when the 'Exit' message is passed.
    if (getIntent().getBooleanExtra("EXIT", false)) {
         finish();
    }

The answer you get to this question from the Android platform is: "Don't make an exit button.  Finish activities the user no longer wants, and the Activity manager will clean them up as it sees fit."

