detecting changes to my program's focus
When other programs are open in Android, how do I know when my program loses focus or gains focus?
I've s开发者_Go百科een the View onFocusChanged
event, but I think that's unrelated (when i use a view changer).
How can this be done?
In your Activity
, you can override the life cycle methods. Specifically, onCreate, onStart, onResume, onPause, onStop and onDestroy. The most useful ones are:
onCreate
: great for one time initialization.onResume
: refresh your data because your app is about to take foreground again.onPause
: save your data and stop anything that doesn't need to be running while your app doesn't have the foreground.
See http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle for a complete overview of these methods and your activities life cycle.
精彩评论