Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix activity leak after configuration change #415

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public class AndroidScreenLifecycleOwner private constructor() :
private val controller = SavedStateRegistryController.create(this)

private var isCreated: Boolean by mutableStateOf(false)
private var isContextResetCallbackRegistered = false

override val savedStateRegistry: SavedStateRegistry
get() = controller.savedStateRegistry
Expand Down Expand Up @@ -140,7 +141,10 @@ public class AndroidScreenLifecycleOwner private constructor() :

@Composable
private fun getHooks(): List<ProvidedValue<*>> {
atomicContext.compareAndSet(null, LocalContext.current)
val context = LocalContext.current
atomicContext.compareAndSet(null, context)
registerContextResetCallback(context)

atomicParentLifecycleOwner.compareAndSet(null, LocalLifecycleOwner.current)

return remember(this) {
Expand Down Expand Up @@ -187,6 +191,20 @@ public class AndroidScreenLifecycleOwner private constructor() :
}
}

private fun registerContextResetCallback(context: Context) {
if (!isContextResetCallbackRegistered) {
val lifecycleOwner = (context as? LifecycleOwner) ?: return
lifecycleOwner.lifecycle.addObserver(
object : DefaultLifecycleObserver {
override fun onDestroy(owner: LifecycleOwner) {
atomicContext.set(null)
isContextResetCallbackRegistered = false
}
}
)
}
}

@Composable
private fun LifecycleDisposableEffect() {
val savedState = rememberSaveable { Bundle() }
Expand Down Expand Up @@ -248,6 +266,7 @@ public class AndroidScreenLifecycleOwner private constructor() :
private val disposeEvents = arrayOf(
Lifecycle.Event.ON_DESTROY
)

public fun get(screen: Screen): ScreenLifecycleOwner {
return ScreenLifecycleStore.get(screen) { AndroidScreenLifecycleOwner() }
}
Expand Down
Loading