When does the *Window focus* change in Android?

人走茶凉 提交于 2019-12-21 09:15:53

问题


In my project, I need to catch the Window focus change. I have logged out the results for all the stages of an activity. When the screen is on, the result is as follows:

02-17 13:50:03.898: DEBUG/InquiryInterface(3829): onCreate screen state : false
02-17 13:50:03.898: DEBUG/InquiryInterface(3829): onStart screen state : false
02-17 13:50:03.898: DEBUG/InquiryInterface(3829): onResume screen state : false
02-17 13:50:08.998: DEBUG/InquiryInterface(3829): onPause screen state : true
02-17 13:50:09.178: DEBUG/InquiryInterface(3829): onWindowFocusChanged : false
02-17 13:50:09.228: DEBUG/InquiryInterface(3829): onStop screen state : false
02-17 13:50:09.228: DEBUG/InquiryInterface(3829): onDestroy screen state : false

onWindowFocusChanged is the method provided by the class Activity. And the value shown in the list is the input value(boolean hasFocus) of the method onWindowFocusChanged.

I have used the following code to get the state of window focus for every state of the method, onCreate, onStart, etc.

@Override
    public void onResume() {
        super.onResume();
        Log.d(TAG, "onCreate screen state : "
                 +String.valueOf(this.hasWindowFocus()));
}

But in the Android documention, I read : the method onResume() is Called when the activity will start interacting with the user. At this point your activity is at the top of the activity stack, with user input going to it.

So in this way, I should get "True" for the window focus within onResume, but not in onPause(). Anyone has an idea why this happens?


回答1:


My understanding of it is the opposite of what you say. onResume() gets called just before your activity gets focus, and onPause gets called just before it loses focus. I could have sworn I read it this way somewhere but I can't seem to find where I saw it in the Activity Lifecycle documentation




回答2:


By the time onResume() is called, your activity already has focus. That's what the docs are telling you.



来源:https://stackoverflow.com/questions/5025311/when-does-the-window-focus-change-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!