views:

2037

answers:

2

Hello. 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.

+8  A: 

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.

CommonsWare
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.
Tom
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().
CommonsWare
When I call finish() in onPause() it just force closes. Ideas?
Tom
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.
CommonsWare
I have taken a screen shot of DDMS and uploaded it here. http://www.2ql.net/uploads/1245827534.pngThis is what DDMS says after a force close when I hit the home button with finish() in onPause()
Tom
You need to call super.onPause() from your own onPause().
CommonsWare
+1  A: 

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

Qlimax