Salga de la aplicación cuando presione el botón de inicio.

I have a security program, so i need to quit the application when the HOME button pressed. But I know the HOME button action cannot be implemented.

Then, I know that I can use


to do it. But I have used

startActivityForResult(intent, 0);

to call a new activity. When the activity called back by

this.setResult(RESULT_OK, intent);

The original activity has already quit.

How can I quit the application when the HOME button pressed, but the startActivityForResult will not quit the activity?

preguntado el 01 de julio de 12 a las 16:07

"I have a security program, so i need to quit the application when the HOME button pressed" -- why do you think that? -

The activity that I need to quit will show many password. I don't want the others "user" will see the owner's password when the application is paused. -

That has nothing whatsoever to do with "quit the application". -

Before showing password, it need to log in. If I used HOME button, the application do not finish, so the user do not need to re-log in. But now I want that it needs to re-log in to show the password. -

Handle this via a timeout mechanism. Users will be very unhappy if they are forced to log in too frequently, and talented programmers want to allow their users to have control over how long an absence will trigger the next re-authentication. Hence, talented programmers would keep a lastVisit static data member and check it in onResume() of their activities. If lastVisit is null or too old, force the user to log in again. Otherwise, just update lastVisit to be the current time. Allow "too old" to be configured via SharedPreferences. -

3 Respuestas

Don't quit the application - that's not how android works.

Instead, in a desired situation, perhaps onPause(), destroy the security credentials that let your app function. On the next run or resume, the user will have to re-authenticate.

Respondido 01 Jul 12, 16:07

Quizás puedas usar onUserLeaveHint() as an indication that you should finish(). This is called when the user intentionally leaves your activity (ie: by pressing HOME key)

Respondido 01 Jul 12, 16:07

As a general rule, you shouldn't be quitting your app like this. It breaks the intuitive workflow most people expect when using an app. However, if you really want to do this, override onPause () y onStop () and implement the code to quit in those. Remember to call super() in your overridden method.

Respondido 01 Jul 12, 16:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.