Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 very 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?

like image 938
Brian Avatar asked Apr 13 '11 23:04

Brian


1 Answers

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

like image 63
triad Avatar answered Nov 04 '22 20:11

triad