1

There is a point in my app where I need to kill the app completely. I use this to do so:

protected void exitApp()
{
    Dialogs.DismissAll();
    finish();
    int pid = android.os.Process.myPid(); 
    android.os.Process.killProcess(pid); 
}

My problem is that this only works sometimes. At other times when this gets called the device screen just turns black and doesn't go to the device home screen. To get to the home screen from this point I have to press the home button. Any ideas why?

(please don't lecture me about how this code is against Android development guidelines. I have read all the information about it and feel that this is the best approach to give the client what they want)

Thanks

spentak
  • 4,627
  • 15
  • 62
  • 90
  • do you have only one activity in this application? – user370305 Sep 09 '11 at 14:20
  • There are multiple activities. So we load the app, then there is a login screen. Once the user logs in, if the back key is pressed while the new activity is loading, this code gets called. In our design, the user should never be able to navigate back to the login screen, the app should just close. So again, the code and problem gets called when the user presses the back key before the new activity has finished loading. – spentak Sep 09 '11 at 14:25
  • You should read: http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon/2034238#2034238 – Peter Knego Sep 09 '11 at 14:25
  • Peter - i have been referred to that post a bajillion times. Not what I need. Thanks. – spentak Sep 09 '11 at 14:26
  • make sure when you land to activity on which the code is written (you mentioned above) then close (finish) all other activity then its work fine. – user370305 Sep 09 '11 at 14:33

4 Answers4

1

Did you check that all activites in the stack are getting destroyed??

jsp
  • 2,546
  • 5
  • 36
  • 63
  • JayP, curious, how do you check if all activities are getting destroyed? – spentak Sep 09 '11 at 14:22
  • What i have done is to use stsrtactivityforresult when opening activity. So when a user hits exit i just pass set result from the activity it is called to next activity till i reach the first one in the stack. Also use finish when calling setresult. – jsp Sep 09 '11 at 14:26
0

you don't need to use finish() or android.os.Process.killProcess(pid) just use :

finishAffinity();

this method finishes this activity as well as all activities immediately below it. that means it really closes the application! here is the complete infromations:

finishAffinity(): Finish this activity as well as all activities immediately below it in the current task that have the same affinity. This is typically used when an application can be launched on to another task (such as from an ACTION_VIEW of a content type it understands) and the user has used the up navigation to switch out of the current task and in to its own task. In this case, if the user has navigated down into any other activities of the second application, all of those should be removed from the original task as part of the task switch.

Note that this finish does not allow you to deliver results to the previous activity, and an exception will be thrown if you are trying to do so.

maanijou
  • 1,067
  • 1
  • 14
  • 23
0

dont remember if that woerks or not. does it kills safely or shows a force close oon kill process. try using this:

ActivityManager aM = (ActivityManager)urActivityInstance.getApplicationContext().getSystemService("activity");
                                aM.restartPackage(urActivityInstance.getApplicationContext().getPackageName());

after this you can add ur killprocess block, so it doesnt appears in task manager.

con_9
  • 2,491
  • 3
  • 23
  • 31
0

In my program I use this code to close app.

MyActivity.this.finish();
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
m4gnum
  • 11
  • 3