Share via


Context.CreateWindowContext Method

Definition

Overloads

CreateWindowContext(Int32, Bundle)

Creates a Context for a non-activity window.

CreateWindowContext(Display, Int32, Bundle)

Creates a Context for a non-android.app.Activity activity window on the given Display.

CreateWindowContext(Int32, Bundle)

Creates a Context for a non-activity window.

[Android.Runtime.Register("createWindowContext", "(ILandroid/os/Bundle;)Landroid/content/Context;", "GetCreateWindowContext_ILandroid_os_Bundle_Handler", ApiSince=30)]
public virtual Android.Content.Context CreateWindowContext (int type, Android.OS.Bundle? options);
[<Android.Runtime.Register("createWindowContext", "(ILandroid/os/Bundle;)Landroid/content/Context;", "GetCreateWindowContext_ILandroid_os_Bundle_Handler", ApiSince=30)>]
abstract member CreateWindowContext : int * Android.OS.Bundle -> Android.Content.Context
override this.CreateWindowContext : int * Android.OS.Bundle -> Android.Content.Context

Parameters

type
Int32

Window type in WindowManager.LayoutParams

options
Bundle

A bundle used to pass window-related options

Returns

A Context that can be used to create non-android.app.Activity activity windows.

Attributes

Remarks

Creates a Context for a non-activity window.

A window context is a context that can be used to add non-activity windows, such as android.view.WindowManager.LayoutParams#TYPE_APPLICATION_OVERLAY. A window context must be created from a context that has an associated Display, such as android.app.Activity Activity or a context created with #createDisplayContext(Display).

The window context is created with the appropriate Configuration for the area of the display that the windows created with it can occupy; it must be used when android.view.LayoutInflater inflating views, such that they can be inflated with proper Resources.

Below is a sample code to <b>add an application overlay window on the primary display:</b>

...
            final DisplayManager dm = anyContext.getSystemService(DisplayManager.class);
            final Display primaryDisplay = dm.getDisplay(DEFAULT_DISPLAY);
            final Context windowContext = anyContext.createDisplayContext(primaryDisplay)
                    .createWindowContext(TYPE_APPLICATION_OVERLAY, null);
            final View overlayView = Inflater.from(windowContext).inflate(someLayoutXml, null);

            // WindowManager.LayoutParams initialization
            ...
            // The types used in addView and createWindowContext must match.
            mParams.type = TYPE_APPLICATION_OVERLAY;
            ...

            windowContext.getSystemService(WindowManager.class).addView(overlayView, mParams);

This context's configuration and resources are adjusted to an area of the display where the windows with provided type will be added. <b>Note that all windows associated with the same context will have an affinity and can only be moved together between different displays or areas on a display.</b> If there is a need to add different window types, or non-associated windows, separate Contexts should be used.

Creating a window context is an expensive operation. Misuse of this API may lead to a huge performance drop. The best practice is to use the same window context when possible. An approach is to create one window context with specific window type and display and use it everywhere it's needed.

After Build.VERSION_CODES#S, window context provides the capability to receive configuration changes for existing token by overriding the android.view.WindowManager.LayoutParams#token token of the android.view.WindowManager.LayoutParams passed in WindowManager#addView(View, LayoutParams). This is useful when an application needs to attach its window to an existing activity for window token sharing use-case.

Note that the window context in Build.VERSION_CODES#R didn't have this capability. This is a no-op for the window context in Build.VERSION_CODES#R.

Below is sample code to <b>attach an existing token to a window context:</b>

final DisplayManager dm = anyContext.getSystemService(DisplayManager.class);
            final Display primaryDisplay = dm.getDisplay(DEFAULT_DISPLAY);
            final Context windowContext = anyContext.createWindowContext(primaryDisplay,
                    TYPE_APPLICATION, null);

            // Get an existing token.
            final IBinder existingToken = activity.getWindow().getAttributes().token;

            // The types used in addView() and createWindowContext() must match.
            final WindowManager.LayoutParams params = new WindowManager.LayoutParams(TYPE_APPLICATION);
            params.token = existingToken;

            // After WindowManager#addView(), the server side will extract the provided token from
            // LayoutParams#token (existingToken in the sample code), and switch to propagate
            // configuration changes from the node associated with the provided token.
            windowContext.getSystemService(WindowManager.class).addView(overlayView, mParams);

After Build.VERSION_CODES#S, window context provides the capability to listen to its Configuration changes by calling #registerComponentCallbacks(ComponentCallbacks), while other kinds of Context will register the ComponentCallbacks to #getApplicationContext() its Application context. Note that window context only propagate ComponentCallbacks#onConfigurationChanged(Configuration) callback. ComponentCallbacks#onLowMemory() or other callbacks in ComponentCallbacks2 won't be invoked.

Note that using android.app.Application or android.app.Service context for UI-related queries may result in layout or continuity issues on devices with variable screen sizes (e.g. foldables) or in multi-window modes, since these non-UI contexts may not reflect the Configuration changes for the visual container.

Java documentation for android.content.Context.createWindowContext(int, android.os.Bundle).

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

CreateWindowContext(Display, Int32, Bundle)

Creates a Context for a non-android.app.Activity activity window on the given Display.

[Android.Runtime.Register("createWindowContext", "(Landroid/view/Display;ILandroid/os/Bundle;)Landroid/content/Context;", "GetCreateWindowContext_Landroid_view_Display_ILandroid_os_Bundle_Handler", ApiSince=31)]
public virtual Android.Content.Context CreateWindowContext (Android.Views.Display display, int type, Android.OS.Bundle? options);
[<Android.Runtime.Register("createWindowContext", "(Landroid/view/Display;ILandroid/os/Bundle;)Landroid/content/Context;", "GetCreateWindowContext_Landroid_view_Display_ILandroid_os_Bundle_Handler", ApiSince=31)>]
abstract member CreateWindowContext : Android.Views.Display * int * Android.OS.Bundle -> Android.Content.Context
override this.CreateWindowContext : Android.Views.Display * int * Android.OS.Bundle -> Android.Content.Context

Parameters

display
Display

The Display to associate with

type
Int32

Window type in WindowManager.LayoutParams

options
Bundle

A bundle used to pass window-related options.

Returns

A Context that can be used to create non-android.app.Activity activity windows.

Attributes

Remarks

Creates a Context for a non-android.app.Activity activity window on the given Display.

Similar to #createWindowContext(int, Bundle), but the display is passed in, instead of implicitly using the #getDisplay() original Context's Display.

Java documentation for android.content.Context.createWindowContext(android.view.Display, int, android.os.Bundle).

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