How to tell when the user is not focused on this activity?
I wasn't exactly sure how to word this question, but I know it's a v开发者_Go百科ery simple one to answer. How can tell when the user is no longer on the activity I want the user to be on. For example, how do I know if the user has unexpectedly pressed HOME or if a phone call is received and interrupts the current activity?
You can check if your Activity has focus with this method:
hasWindowFocus()
If you want to capture when the user leaves the activity, you'd want implement this in your Activity:
protected void onPause() {
super.onPause();
// Code here...
}
Also there is more info about activity life cycle here: http://developer.android.com/reference/android/app/Activity.html
Check the Activity life cycle:
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
If the user leaves the Activity for any reason, onPause() will be called.
精彩评论