Reputation: 477
What does Jetpack Compose do if you put a remember
statement inside some conditional branch?
For example, take the following code:
@Composable
fun MyScreen(repository: Repository?) {
if (repository != null) {
val myState by remember(repository) { repository.myFlow }.collectAsState()
/* ... */
}
}
Suppose the first composition has some valid repository
parameter, but then we have a recomposition with repository = null
. The remember
statement will not be called then; will the remembered value be cleared? (Otherwise this would be a memleak, would it not?)
Upvotes: 2
Views: 2055
Reputation: 6863
I think it will be lost. I believe so because what you are passing into remember
as a parameter, is basically a key
, and if the key changes, remember will be called again. However, since in your case, when the key changes, i.e., when the repository is null, the very block of remember will not be executed, so it will be just like the first time the composable was created. Nothing is remembered, and no data is stored because of no execution
Here's your confirmation:
Some reference,
Upvotes: 2