Hello, I am returning a url in string format from...
# compose
n
Hello, I am returning a url in string format from my
ViewModel
in return of text click and subscribe the output in composable function as follows;
Copy code
val url by viewModel.output.url.subscribeAsState(initial = null)
However, since the url is always the same string, only the first click opens browser, subsequent ones don’t do anything due to
subscribeAsState
How do I make this work properly for identical strings?
z
You’re conflating states and events. What you want is an event that says “open this URL”, not a state that says “this is the current URL”.
You need to collect your
url
observable explicitly and open the browser there.
n
Thank you for the answer! Do you have an example of how to explicitly collect observables in composables? I think you mean I shouldn’t use
subscribeAsState
y
Search for "compose one shot events" you'll find lots of blog posts
n
Thank you!