# Application and window lifetime

Give each resource a clear end point. A desktop window can close while the application continues; a mobile process may disappear without a final cleanup callback. Build your page behavior around the lifetime it actually has.

## Start the application once

The starter's `Application` derives from `Xui.Core.Abstract.Application`. Its `Start()` method opens the first window after the runtime initializes. `Run()` creates the platform run loop; some desktop implementations block there, while hosts that already own a loop can return immediately.

The [service composition example](../services/index.md) shows a complete application using `CreateAndShowMainWindowOnce<TWindow>()`. That helper creates the window scope, sets `DestroyOnClose`, and arranges to call `Quit()` after the main window closes. Use `CreateAndShowOnce<TWindow>()` for an additional window that should not terminate the app when it closes.

## Distinguish closing from hiding

`Window.Show()` asks the platform to show the native window. Normal closure reaches `Window.Closed()`, which disposes the window when `DestroyOnClose` is true. `Window.Dispose()` also closes a still-open native window, clears focus, and processes its disposal queue. Treat a disposed window as finished; create a new instance to reopen that experience.

Set `DestroyOnClose = false` only when you deliberately retain and reuse that window. You then own its eventual disposal. Retaining a closed window also retains its view tree and any state you left attached.

`DisposeQueue` is an ordered list. Register the object that owns a resource, avoid registering the same owner twice, and do not assume reverse-order stack semantics.

## Use view lifetime for page work

Start subscriptions or cancellable page operations in `OnActivate`; stop them in `OnDeactivate`. A view may become inactive while still attached, so attachment alone is not proof that it should keep animating or receiving external updates.

Use `OnAttach`/`OnDetach` for ownership tied to tree membership. Let framework containers perform these transitions when adding, removing, or replacing children. Calling lifecycle methods manually skips the framework's flags and descendant traversal.

The current base `Window.Dispose()` does not remove `RootView.Content`. If your window owns page work that must receive deactivation at close, clear `RootView.Content` in your window's disposal override before calling the base implementation. Do not assume closing every host automatically runs your page cleanup.

## Plan for platform differences

`Application.Quit()` delegates to the runtime. It is not a portable way to close a browser tab or terminate an iOS app. `Application.OnExit()` runs only on hosts with a definite exit point; browser and iOS code must not rely on it for saving important data.

Persist changes at meaningful application boundaries, not only on process exit. The browser host currently supports one top-level window per canvas. Use in-window navigation or overlays there, and reserve additional native windows for hosts that support them.
