How to kill an application with all its actions?

Possible duplicate:
Exit the application - frowned?

I want to offer the user the opportunity to exit the application, since I need to delete some confidential data that is stored in SharedPreferences while the application needs it.

As soon as the user wants to log out, the password in SharedPreferences should be wiped, and, of course, all actions of the application should be closed (there is no sense in running them without a known password - they will be broken).

How can i do this?

System.exit(0) and finish() only exit from the current activity is useless. I know there is a taskmanager application. How it's done? He is able to kill the whole application ...

+43
android kill
Jun 23 '10 at 21:24
source share
6 answers

When you use the finish () method, it does not completely close the process; it runs in the STILL background.

Please use this code in the Main activity (Please do not use in each event or in the "Events" section):

 @Override public void onBackPressed() { android.os.Process.killProcess(android.os.Process.myPid()); // This above line close correctly } 
+80
May 15 '12 at 8:38 a.m.
source share

You are right: calling finish() will only exit the current activity, and not the entire application. however there is a workaround for this:

Every time you start an action, run it using startActivityForResult(...) . When you want to close the entire application, you can do something like this:

 setResult(RESULT_CLOSE_ALL); finish(); 

Then define each onActivityResult(...) activity onActivityResult(...) , so when the action returns with the value RESULT_CLOSE_ALL , 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 lead to a cascading effect that closes all actions.

In addition, I support CommonsWare on its assumption: store the password in a variable so that it is destroyed when the application is closed.

+32
Jun 24 '10 at 1:25
source share

When the user wants to exit all open actions, they must click the button that loads the first action that starts when the application 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 actions except LoginActivity. LoginActivity is the first action that occurs when a user launches a program. Then put this code inside LoginActivity onCreate to signal when it should destroy itself when an Exit message is sent.

  if (getIntent().getBooleanExtra("EXIT", false)) { finish(); } 

The answer to this question from the Android platform is: β€œDon't do the exit button. Finish the actions that the user no longer wants, and the activity manager will clear them as they see fit.”

+12
May 02 '12 at 4:20
source share

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

Why?

As soon as the user wants to log out, the password in SharedPreferences should be wiped and, of course, all activity of the application should be closed (it makes no sense to run them without a known password - they fail).

Even better: do not enter a password in SharedPreferences . Hold it in a static data item. The data will naturally disappear when all actions in the application are deleted (for example, the BACK button) or otherwise destroyed (for example, expelled from RAM to free up space for other actions after the user presses HOME).

If you need some kind of proactive "password for flash", just set the static data member to null , and your actions will check this member and take appropriate actions when it is null .

+7
Jun 23 '10 at 21:42
source share

Using the onBackPressed() method:

 @Override public void onBackPressed() { android.os.Process.killProcess(android.os.Process.myPid()); } 

or use finish() method, I have something like

 //Password Error, I call function Quit(); protected void Quit() { super.finish(); } 

With super.finish () you close superclass activity.

+6
Jun 23 2018-10-23T00:
source share

My understanding of the framework for Android apps is that this is not specifically permitted. The application closes automatically when it does not contain any more current actions. Trying to create a kill button seems to contradict the intended design of the application system.

To get the desired effect, you can initiate various actions using the startActivityForResult () function, and the exit button send a result that tells the parent activity to finish (). Then this activity can send the same result as part of its onDestroy (), which will return to the main action and not lead to the launch of actions, which should lead to the closure of the application.

+3
Jun 23 '10 at 21:35
source share



All Articles