Cool Khan
Cool Khan

Reputation: 361

How can i close an activity with the back button of device?

i want to close an activity with the back button of my device... any suggestion???

Upvotes: 1

Views: 782

Answers (3)

Juri
Juri

Reputation: 32900

Generally speaking, Android's philosophy is to not include an explicit exit functionality. If you're handling resources properly, i.e. activating them during the Resume() and releasing them during the Pause() event, then you're just fine. Your activity will stay there and the system will decide on whether to remove it or to recycle it in case it comes to the foreground again later on.

Reto Meier wrote an interesting article about whether to include exit buttons/functionalities: http://blog.radioactiveyak.com/2010/05/when-to-include-exit-button-in-android.html

As also Roman Guy said in his popular ListView talk at I/O2010: Don't try to be smarter than the system but just aim to use it the way it is intended, because you most probably make the situation worse than better. These guys are putting a lot of effort in making things perform more optimal with each new release of the OS.

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006614

The current activity is automatically "closed" (a.k.a., destroyed) by the BACK button on your device.

Upvotes: 4

anon
anon

Reputation:

Register an KeyListener and implement [OnKey][1]. There check if the back button is pressed. If the back button is pressed, call the OnStop method.

But be aware that this could violate the lifecycle principle in Android

[1]: http://developer.android.com/reference/android/view/View.OnKeyListener.html#onKey(android.view.View, int, android.view.KeyEvent)

Upvotes: 0

Related Questions