views:

109

answers:

4

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 SharesPreferences 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 ...

Thanks.

A: 

My understanding of the Android application framework is that this is specifically not permitted. An application is closed automatically when it contains no more current activities. Trying to create a "kill" button is apparently contrary to the intended design of the application system.

To get the sort of effect you want, you could initiate your various activities with startActivityForResult(), and have the exit button send back a result which tells the parent activity to finish(). That activity could then send the same result as part of its onDestroy(), which would cascade back to the main activity and result in no running activities, which should cause the app to close.

tlayton
+1  A: 

which is stored in the SharesPreferences as long as the application needs it.

Why?

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

Even better: don't put the password in SharedPreferences. Hold onto it in a static data member. The data will naturally go away when all activities in the app are exited (e.g., BACK button) or otherwise destroyed (e.g., kicked out of RAM to make room for other activities sometime after the user pressed HOME).

If you want some sort of proactive "flush password", just set the static data member to null, and have your activities check that member and take appropriate action when it is null.

CommonsWare
Thanks. That worked, but as soon as there is a crash in any activity and it will be NULL, even other activities will be still there. Well okay ... I guess that's fine
Nils
A: 

use the finish() function, I have someting like

//Password Error, I call function
    Quit();             


    protected void Quit() {
        super.finish();
    }

with super.finish() you close the app and all of their activities!

Jorgesys
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

mtmurdock