From f59111f7ae55c18b72dc7b1950738139f78ad077 Mon Sep 17 00:00:00 2001 From: Mattias Buelens Date: Mon, 9 Sep 2024 16:39:46 +0200 Subject: [PATCH] Don't destroy on dispose when using rememberPlayer(THEOplayerView) --- .../com/theoplayer/android/ui/UIController.kt | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/ui/src/main/java/com/theoplayer/android/ui/UIController.kt b/ui/src/main/java/com/theoplayer/android/ui/UIController.kt index c226274..7385215 100644 --- a/ui/src/main/java/com/theoplayer/android/ui/UIController.kt +++ b/ui/src/main/java/com/theoplayer/android/ui/UIController.kt @@ -457,6 +457,13 @@ fun rememberPlayer(config: THEOplayerConfig? = null): Player { val context = LocalContext.current remember { THEOplayerView(context, config) } } + + DisposableEffect(theoplayerView) { + onDispose { + theoplayerView?.onDestroy() + } + } + return rememberPlayerInternal(theoplayerView) } @@ -480,6 +487,17 @@ fun rememberPlayer(config: THEOplayerConfig? = null): Player { * That is, it automatically calls [THEOplayerView.onPause] and [THEOplayerView.onResume] * whenever the current activity is [paused][Activity.onPause] or [resumed][Activity.onResume]. * + * The [THEOplayerView] is **not** automatically destroyed when the composition is disposed. + * If you need this, use a [DisposableEffect]: + * ```kotlin + * val player = rememberPlayer(theoplayerView) + * DisposableEffect(theoplayerView) { + * onDispose { + * theoplayerView.onDestroy() + * } + * } + * ``` + * * @param theoplayerView the existing THEOplayer view */ @Composable @@ -505,12 +523,6 @@ internal fun rememberPlayerInternal(theoplayerView: THEOplayerView?): Player { internal fun setupTHEOplayerView(theoplayerView: THEOplayerView): THEOplayerView { var wasPlayingAd by remember { mutableStateOf(false) } - DisposableEffect(theoplayerView) { - onDispose { - theoplayerView.onDestroy() - } - } - val lifecycle = LocalLifecycleOwner.current.lifecycle DisposableEffect(lifecycle, theoplayerView) { val lifecycleObserver = LifecycleEventObserver { _, event ->