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.OnStop does not work because that is called even when switching activities within my app.
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.OnStop does not work because that is called even when switching activities within my app.
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).
I believe you could use:
onWindowsFocusChanged(boolean hasFocus)
from your Activity.
I have written a utility method for this, it's part of the Droid-Fu library and BetterActivity.
Use it like this:
protected void onPause() {
if (isApplicationBroughtToBackground()) {
...
}
}
Some introductory articles about Droid-Fu on my blog.