Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to kill an application with all its activities? [duplicate]

Tags:

android

kill

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

like image 479
Nils Avatar asked Jun 23 '10 21:06

Nils


People also ask

How do I kill apps on Android?

Tap and hold on the application and swipe it to the right. This should kill the process from running and free up some RAM. If you want to close everything, press the "Clear All" button if its available to you.

Which method is used to kill the activity?

The onStop() and onDestroy() methods get called, and Android destroys the activity. A new activity is created in its place.

How do you kill a task on Android?

If an app or service's process is frozen on your device, use the Force Stop button to kill the process. You can open the Manage Applications screen on your Android and tap a process to view details about its performance and resource usage. The process's details screen contains the Force Stop button.


2 Answers

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
}
like image 74
Thirumalvalavan Avatar answered Oct 19 '22 19:10

Thirumalvalavan


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.

like image 40
mtmurdock Avatar answered Oct 19 '22 19:10

mtmurdock