Killing android application on pause
Asked Answered
T

3

12

I have an application which I would like to be fully disabled/closed when it is paused (IE. When the user presses the Home, End (call) and Back button I would like the application to be closed, instead of being saved in the history stack).

How do I do this....?

Thanks.

Taproom answered 21/6, 2009 at 21:28 Comment(0)
C
25

Implement onPause() in your activity and call finish() on your activity. Bear in mind, though, that this will occur on every pause, including dialogs, incoming calls, users activating a Notification. You might want to consider doing finish() in onStop(), which would at least solve the dialog problem.

Also, bear in mind that users will may get confused when using your app, thinking it has crashed since it is gone when they try to get back to it.

Cray answered 21/6, 2009 at 23:4 Comment(6)
Thanks for the reply, I will try this. The application needs to work like this as I would only like the app to be running when the user has it in the foreground and leaving it running in the background may cause extreme loss of battery life.Taproom
You should not have to kill off the whole activity, though, in that case. Just disable whatever it is that you are doing that is chewing up the battery life, and re-enable it in onStart() or onResume().Cray
When I call finish() in onPause() it just force closes. Ideas?Taproom
If by "force close" you mean you get a crash dialog, what does the stack trace tell you? You can get the Java stack trace via adb logcat, standalone DDMS, or the DDMS perspective in Eclipse.Cray
I have taken a screen shot of DDMS and uploaded it here. 2ql.net/uploads/1245827534.png This is what DDMS says after a force close when I hit the home button with finish() in onPause()Taproom
You need to call super.onPause() from your own onPause().Cray
G
13

you can easily do that by setting true the "noHistory" attribute in to your activity element, in the manifest

http://developer.android.com/guide/topics/manifest/activity-element.html#nohist

Garlen answered 9/8, 2010 at 15:46 Comment(0)
M
3

You know how you have an OnCreate() method in your activity which performs actions when you start. You need to add something like:

@Override

protected void onPause(){
finish();

        super.onPause();

}

in your activity to add actions before it starts

in this case the

finish(); 

command is what you want to execute before your activity pauses.

Moonwort answered 31/1, 2011 at 21:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.