开发者

How to tell when the user is not focused on this activity?

开发者 https://www.devze.com 2023-02-24 06:20 出处:网络
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 examp

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消