Many people want to process the event when the activity is loaded. For example, if a popwindow or another window is displayed, but the oncreate method processes the time, the program will report an error: the general idea is that the activity has not been fully loaded and cannot handle the pop-up window event. In fact, it is not just a pop-up window, as long as the activity is used. this event cannot be processed before the activity is loaded. The reason is obvious: the display of popwindow or dialog must be attached to a parent activity. Therefore, after the activity is loaded, neither of them can be displayed.
Fortunately, Android provides developers with a method to solve this problem. This method is onwindowfocuschanged ()
The description is as follows:
Called when the currentWindow
Of
The activity gains or loses focus. This is the best indicator of whether this activity is visible to the user. The default implementation clears the key tracking state, so shocould always be called.
Note that this provides information about global focus state, which is managed independently of activity lifecycles. As such, while focus changes will generally have some relation to Lifecycle changes (an activity that is
Stopped will not generally get window focus), You shoshould not rely on any particle order between the callbacks here and those in the other lifecycle methods suchonResume()
.
As a general rule, however, a resumed activity will have window focus... unless it has displayed other dialogs or popups that take input focus, in which case the activity itself will not have focus when the other windows
Have it. Likewise, the system may display system-level windows (such as the status bar notification panel or a system alert) which will temporarily take Window input focus without pausing the foreground activity.
Java code
- @ Override
- Public void onwindowfocuschanged (Boolean hasfocus)
- {
- If (hasfocus)
- {
- }
- }