Android: How do I know if Activity.onStop() is being called because the user is going back to Home? [duplicate]
Possible Duplicate:
Android: Detect when another Activity is launched (or your activity loses focus)
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?
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).
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.
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.
精彩评论