Skip to content

Conversation

@jayohms
Copy link
Contributor

@jayohms jayohms commented Jan 9, 2026

Allow BridgeComponent instances to implement the BridgeComponentFragmentLifecycle interface to receive the destination Fragment onViewCreated() and onDestroyView() callbacks. This is useful to launch coroutines tied to the Fragment's viewLifecycleOwner so they can be reliably relaunched when the view is recreated.

Its use looks this:

internal class MyComponent(
    name: String,
    bridgeDelegate: BridgeDelegate<HotwireDestination>
) : BridgeComponent<HotwireDestination>(name, bridgeDelegate), BridgeComponentFragmentLifecycle {
    private val fragment = bridgeDelegate.destination.fragment

    override fun onViewCreated() {
        fragment.viewLifecycleOwner.lifecycleScope.launch {
            fragment.viewLifecycleOwner.lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
                // run coroutine, such as collecting a Flow
            }
        }
    }

    override fun onDestroyView() {
        // Perform any cleanup
    }

    // remaining component code
}

…nation Fragment's onViewCreated() and onDestroyView() callbacks. This is useful to to launch coroutines tied to the Fragment's `viewLifecycleOwner` so they can be relaunched when the view is recreated.
@jayohms jayohms requested a review from mbarta January 9, 2026 15:38
Copy link
Collaborator

@mbarta mbarta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@jayohms jayohms merged commit 588d69c into main Jan 9, 2026
1 check passed
@jayohms jayohms deleted the bridge-delegate branch January 9, 2026 15:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants