What is the useEffect correspondent in android compose component

I have a master-detail app for android using kotlin and jetpack compose components. The flow in the app should be:

  1. Open app
  2. The app starts in the master view with a list having all items that were retrieved from a api using a view model.
  3. When an item is clicked, we navigate towards the detail screen which should call only once findItem(id) function which is implemented in the view model of the screen.

The problem is that I don t know how to call only at the first render the function from the view model. Right now , there is an infinite loop inside which the find function is called because I store the item provided by the view model inside an observableState. Every time that changes, the component is rerendered and the find function is called again. How can I call it only once?

Upvotes: 1

Views: 1394

Answers (2)

VariabileAleatoria
VariabileAleatoria

Reputation: 160

You can get the id from savedStateHandle inside the screen viewModel and call the fetch from the init block of the viewModel. This will be called once on page first composition.

Upvotes: 0

Jakoss
Jakoss

Reputation: 5275

LaunchedEffect(Unit) {
    // this will run once per composition 
}

Upvotes: 3

Related Questions