spierce7

    spierce7

    1 year ago
    I’m using an
    AndroidView
    composable to embed a native Android View into Jetpack Compose. However, I’d when a certain event happens on my model, I’d like to re-create the
    AndroidView
    , removing the old, and replacing it with a newly created one. How would I go about doing that?
    jim

    jim

    1 year ago
    Take a look at
    androidx.compose.runtime.key
    But I'm a bit suspicious of your framing "when an event occurs", as it tends to indicate that you are thinking in an imperative way instead of a declarative one. Instead, consider how your data model needs to be updated when the event occurs, and how that data model gets rendered to the screen. This might be "when a user clicks the 'new' button, I create a new empty contact card with a new UUID", and then the key around your AndroidView would be
    key(uuid)
    . Without more details, I can't tell you what your key should be, but I can tell you that your question implies you're thinking about the problem imperatively.
    spierce7

    spierce7

    1 year ago
    Thanks. I’ll look at androix.compose.runtime.key
    But I’m a bit suspicious of your framing
    Good call. I’m using exoplayer, and exoplayer seems to have a bug when updating playlists. Sometimes it gets stuck and doesn’t play, even after calling
    play
    . The best way to guarantee this doesn’t happen I’ve found is just to reload the screen. Another way, is just to re-create the Exoplayer View.
    jim

    jim

    1 year ago
    It's quite a blunt instrument, but yeah, you can increment an integer or generate a new UUID into the key whenever you want to reload the widget.
    Adam Powell

    Adam Powell

    1 year ago
    Can you please file a bug for the player getting stuck? We can pass it along to the exoplayer team and hopefully you won't have to use that workaround for long.