> ## Documentation Index
> Fetch the complete documentation index at: https://docs.kontext.so/llms.txt
> Use this file to discover all available pages before exploring further.

# Following the screen's lifecycle

> Who owns the episode's VideoAds, and when to destroy it: an Activity, a Fragment, a ViewModel or Compose.

A `VideoAds` belongs to the episode the viewer watches: create it when the screen opens, and `destroy()` it with whatever owns it.

* Owned by a `ViewModel`: `destroy()` in `onCleared()`, and in every new Activity `track` its player. The ads survive a rotation, and so does the ad on screen, which outlives the Activity: set its listener from the current screen, again after every recreation, and clear it in `onDestroy()`, so `onAdEnded` never reaches a destroyed Activity (a listener left from the old screen also keeps it alive).

  ```kotlin theme={null}
  override fun onCreate(savedInstanceState: Bundle?) {
      // …
      model.ads.track(player)
      model.pauseAd?.listener = this         // this screen hears the ad's end
  }

  override fun onDestroy() {
      super.onDestroy()
      model.pauseAd?.listener = null         // never a destroyed Activity
  }
  ```
* Owned by the Activity, a Fragment or a Compose `DisposableEffect`: `destroy()` in its `onDestroy()` (`onDispose`), every time. A rotation or another configuration change destroys and recreates the Activity, and the ads with it: declare `android:configChanges` for rotation, as the example app does, so a rotation does not recreate it.
* A `VideoAds` its owner forgot is never released: it holds its player adapter, its ads and their listeners.
* A linear ad runs in the SDK's own Activity, over yours. Yours is stopped meanwhile and may be destroyed and recreated (a rotation, the system reclaiming memory); with the `VideoAds` in a `ViewModel`, or for a rotation in an Activity that declares `android:configChanges`, the ad keeps playing, and its end reaches the listener it was shown with.
* A player Activity with `launchMode="singleInstance"` is not supported for linear ads: the ad opens in another task, and the viewer returns there. Use `standard`, `singleTop` or `singleTask`.
* The [example app](https://docs.kontext.so/video/sdk/kotlin/example.md) keeps its player Activity across rotation with `android:configChanges` and calls `ads.destroy()` in every `onDestroy()`.
* The ad holds its listener until `onAdEnded` has run, so a listener that is your Activity stays reachable until the ad ends.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.