Skip to content

How to observe current screen in backstack? #199

Answered by KovalevAndrey
berhram asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @berhram you can implement your own extension that transforms BackStack element into a State like this:

val <T : Any> BackStack<T>.activeElementState: State<T?>
    @Composable
    get() = elements.map { it.activeElement }.collectAsState(initial = null)

and your client code would look like this:

val activeElement by backStack.activeElementState

LaunchedEffect(activeElement) {
    // do something with your active element
}

In the meantime we can add support of this to the library

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@berhram
Comment options

Answer selected by berhram
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants