开发者

Android app doesn't close

开发者 https://www.devze.com 2023-04-04 07:02 出处:网络
There is a point in my app where I need to kill the app completely.I use this to do so: protected void exitApp()

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


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


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.


In my program I use this code to close app.

MyActivity.this.finish();


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消