Pagin V3-单击列表项目并回来时丢失状态 - JetPack撰写

发布于 2025-02-01 15:07:19 字数 1234 浏览 3 评论 0原文

我正在使用分页库中的分页列表在底部的NAV栏中的懒惰列中显示一个分页列表,其中有4个选项卡说“ HomeScreen”。 单击其项目时,它将以正常意图进行下一个活动。但是,当我向后键并回到具有分页列表的组合时。它开始重新编译,并从第一个显示列表。从堆栈上的其他活动回来时,如何保留合成状态。 只有单击分页的列表项目并导航并按下键重新访问HomeScreen,请重新组合列表,并且该列表从第一,从第一, 带有Noraml Lazy列表的其他标签在Back Key Revisit上正常工作。 从详细信息页面回到主屏幕时,请帮助维护列表状态

// getting paged data inside composable
    val pagedList =  viewModel.getPagedList().collectAsLazyPagingItems()


//navigating on click item
    val context = LocalContext.current
 context.openActivity(
                                    DetailsScreen::class.java,
                                    extras = {
                                        putInt("id", 5)
                                    })

//open activity extension
fun <T> Context.openActivity(
    it: Class<T>,
    shouldFinish: Boolean = false,
    extras: Bundle.() -> Unit = {},
    isOutsideActivity: Boolean = false
) {
    val intent = Intent(this, it)
    if (isOutsideActivity) {
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
    }
    intent.putExtras(Bundle().apply(extras))
    startActivity(intent)
    if (shouldFinish)
        this.findActivity()?.let {
            it.finish()
        }
}

I am displaying a paginated list using paging library in a lazy column in a bottom nav bar with 4 tabs say HomeScreen.
When its item is clicked it will to next activity with normal intent . But when i press back key and come back to the composable which has the paginated list . it starts recomposing and the list is shown from the first. How to retain the state of the composable when coming back from other activity on stack.
Only when clicking on a paginated list item and navigating and press back key to revisit HomeScreen the composable with paging list is recomposing and the list is show from first,
other tabs with noraml lazy list is working perfectly on back key revisit.
Please help maintain the list state when revisiting the screen when come to Home screen back from Details page

// getting paged data inside composable
    val pagedList =  viewModel.getPagedList().collectAsLazyPagingItems()


//navigating on click item
    val context = LocalContext.current
 context.openActivity(
                                    DetailsScreen::class.java,
                                    extras = {
                                        putInt("id", 5)
                                    })

//open activity extension
fun <T> Context.openActivity(
    it: Class<T>,
    shouldFinish: Boolean = false,
    extras: Bundle.() -> Unit = {},
    isOutsideActivity: Boolean = false
) {
    val intent = Intent(this, it)
    if (isOutsideActivity) {
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
    }
    intent.putExtras(Bundle().apply(extras))
    startActivity(intent)
    if (shouldFinish)
        this.findActivity()?.let {
            it.finish()
        }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文