tags:

views:

198

answers:

3

I need be able to tell if Activity.onStop() was called because my application is moving to a new activity, or if it was closed because the user pressed the "Home" key or hit the back button from the bottom of the activity stack.

The reason is because I need to know when it is appropriate to shut off music that is playing in my application (A game). There is nothing more annoying than hitting home and having something playing music in the background.

However, Activity.OnStop() is called for each activity change, and I don't want to suspend music when moving between multiple activities in my app. I just can't find a way to differentiate between going home and going to a internal activity.

Am I perhaps hooking into the wrong events?

Any advice?

A: 

There is no onClose(). There is onPause(), onStop(), and onDestroy(), but no onClose(). You can call isFinishing() to see if the activity is being paused because of a back button (or finish() call). isFinishing() will return false if the activity will be sticking around (e.g., user pressed HOME, user takes a phone call).

CommonsWare
Doh, I meant onStop, I just wrote this rather late.
FlySwat
Surely you should have added this as a comment commonsware.com. This is not an answer!
Beau Martínez
No, using isFinishing() to determine if the activity is finishing or not is an answer.
CommonsWare
A: 

I guess you could add a View.OnKeyListener that would catch the Home key press in it's public boolean onKey(View v, int keyCode, KeyEvent event){}, shut the music down and return false telling that the event hasn't been consumed.

alex
KeyListeners don't bubble up the system keys.
FlySwat
+1  A: 

didn't you ask the same question in another thread? see my answer there, you can either use that library or copy the method is uses to your own code, it's licensed under AC2.

Matthias