简体   繁体   English

如何阻止屏幕在 Jetpack Compose 中重新合成?

[英]How can I stop a screen from recomposing in Jetpack Compose?

I have navigation drawer where I have two items:我有导航抽屉,其中有两个项目:

ModalNavigationDrawer(
    drawerState = drawerState,
    drawerContent = {},
    content = {
        Scaffold(
            topBar = {},
            content = { padding ->
                Box(
                    modifier = Modifier.fillMaxSize().padding(padding)
                ) {
                    when (viewModel.selectedItem) {
                        items[0] -> Home()
                        items[1] -> Products()
                    }
                }
            }
        )
    }
)

Where items is a list that is initialized like this:其中items是这样初始化的列表:

val items = listOf(
    Item(Icons.Default.Home, "Home"),
    Item(Icons.Default.List, "Products")
)

And selectedItem is initialized inside the ViewModel like this并且selectedItem像这样在 ViewModel 内部初始化

var selectedItem by mutableStateOf(items[0])

Where, Home has this content:哪里, Home有这个内容:

fun Home() {
    Column(
        modifier = Modifier.fillMaxSize()
    ) {
        PopularProducts(
            popularProductsContent = { popularProducts ->
                HorizontalContent(
                    products = popularProducts
                )
            }
        )
    }
}

And Products this:Products这个:

fun Products(
    viewModel: MainViewModel = hiltViewModel(),
) {
    LaunchedEffect(Unit) {
        viewModel.getAllProducts()
    }
    Products { products ->
        Column {
            LazyColumn(
                modifier = Modifier
                    .fillMaxWidth()
                    .wrapContentHeight(),
                contentPadding = PaddingValues(8.dp)
            ) {
                items(products) { product ->
                    ProductCard(
                        product = product
                    )
                }
            }
        }
    }
}

When I launch the app, Home item is loaded.当我启动应用程序时,会加载Home项目。 If I navigate away from it, and I get back, everything is alright, the page isn't recomposed.如果我离开它,然后我回来,一切都很好,页面没有重新组合。 But if I select Products , and I navigate to the product details, for example, when I navigate back, the screen is recomposed (it blinks).但是,如果我 select Products ,并且我导航到产品详细信息,例如,当我导航返回时,屏幕会重新组合(它会闪烁)。 Basically another request is made.基本上提出了另一个请求。 How can I stop this from happening?我怎样才能阻止这种情况发生?

PS I use Hilt Navigation. PS 我使用 Hilt Navigation。

This is because your LaunchedEffect will be getting called again which is triggering your products to reload.这是因为您的LaunchedEffect将再次被调用,这会触发您的产品重新加载。

You just need to cache your products somehow so that reload and reset isn't happening, there are many ways to do that with increasing complexity and architecture but the simplest fix would be just to call viewModel.getAllProducts() in your MainViewModel init instead of having the LaunchedEffect .您只需要以某种方式缓存您的产品,这样就不会发生重新加载和重置,随着复杂性和架构的增加,有很多方法可以做到这一点,但最简单的解决方法是在MainViewModel init中调用viewModel.getAllProducts()而不是具有LaunchedEffect

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM