Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does the *Window focus* change in Android?

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?

like image 744
Mathieu Avatar asked Feb 17 '11 05:02

Mathieu


1 Answers

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

like image 110
FoamyGuy Avatar answered Oct 10 '22 15:10

FoamyGuy