Share via


Activity.OnWindowFocusChanged(Boolean) Method

Definition

Called when the current Window of the activity gains or loses focus.

[Android.Runtime.Register("onWindowFocusChanged", "(Z)V", "GetOnWindowFocusChanged_ZHandler")]
public virtual void OnWindowFocusChanged (bool hasFocus);
[<Android.Runtime.Register("onWindowFocusChanged", "(Z)V", "GetOnWindowFocusChanged_ZHandler")>]
abstract member OnWindowFocusChanged : bool -> unit
override this.OnWindowFocusChanged : bool -> unit

Parameters

hasFocus
Boolean

Whether the window of this activity has focus.

Implements

Attributes

Remarks

Called when the current Window of the activity gains or loses focus. This is the best indicator of whether this activity is the entity with which the user actively interacts. The default implementation clears the key tracking state, so should always be called.

Note that this provides information about global focus state, which is managed independently of activity lifecycle. 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 should not rely on any particular order between the callbacks here and those in the other lifecycle methods such as #onResume.

As a general rule, however, a foreground 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.

Starting with android.os.Build.VERSION_CODES#Q there can be multiple resumed activities at the same time in multi-window mode, so resumed state does not guarantee window focus even if there are no overlays above.

If the intent is to know when an activity is the topmost active, the one the user interacted with last among all activities but not including non-activity windows like dialogs and popups, then #onTopResumedActivityChanged(boolean) should be used. On platform versions prior to android.os.Build.VERSION_CODES#Q, #onResume is the best indicator.

Java documentation for android.app.Activity.onWindowFocusChanged(boolean).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to

See also