Android - How to exit an app when user press the home button?

Varundroid picture Varundroid · Apr 30, 2011 · Viewed 12.6k times · Source

I want to know how to exit an app when user press the Home Button.

As far as i know that Home Button moves the running app in background and puts Launcher process in front.

I know i can use finish() but i don't know where i should call it because i have no idea which function is going to get a call when user will press the Home Key.

Thanks.

Answer

Kevin TeslaCoil picture Kevin TeslaCoil · Apr 30, 2011

Depending on what you want to do, overriding onUserLeaveHint might be the best bet:

http://developer.android.com/reference/android/app/Activity.html#onUserLeaveHint()

This will let your app know that your app is being exited because the user chose to switch apps (Like by hitting the Home button or selecting a notification). This hint function will not be called if an incoming phone call (or similar) is causing your app to go to the background, as the user didn't initiate that action.