Like the title says, I need to detect when my app loses focus because another app is launched (Phone call comes in, or user hits Home etc).
Overriding Activity.OnSto
AFAIK Android offers no facility for this. You may be able to track this yourself (e.g., if onStop()
in one of your activities is called, and onStart()
in another of your activities is not called within X period of time, presumably some other app's activity is in the foreground).
With ICS upwards this may be possible.
This is taken from the android site:
To be notified when the user exits your UI, implement the onTrimMemory() callback in your Activity classes. You should use this method to listen for the TRIM_MEMORY_UI_HIDDEN level, which indicates your UI is now hidden from view and you should free resources that only your UI uses.
Notice that your app receives the onTrimMemory() callback with TRIM_MEMORY_UI_HIDDEN only when all the UI components of your app process become hidden from the user.
See this page for full details http://developer.android.com/training/articles/memory.html
I believe you could use:
onWindowsFocusChanged(boolean hasFocus)
from your Activity.