体系结构导航组件:每次都会调用onCreateView

时间:2019-02-26 06:27:56

标签: android androidx fragment-backstack android-architecture-navigation

每次创建片段实例并在按下时重新加载。

如何克服这个问题?

在nav控制器中没有适当的后堆栈是一个巨大的生产力问题。希望它是一项缺少的功能或有针对性的解决方法... IMO这是必须的,必须尽早引入,而不是稍后引入(但是:https://issuetracker.google.com/issues/109856764说,我们不会允许在可预见的将来定制交易类型(例如隐藏/显示等)。):(

Nav-Framework is nice, however, it create fragment's view every time

有人可以解决吗?

我的代码是使用导航

进行导航
object NavigationHelper : Navigational {


override fun moveDownWithPop(view: View,id: Int,popStackId: Int) {
    Navigation.findNavController(view).navigate(id,
            null,
            NavOptions.Builder()
                    .setPopUpTo(popStackId,
                            true).build()
    )
}

override fun moveDownWithDataPop(view: View,id: Int,popStackId: Int,bundle : Bundle) {
    Navigation.findNavController(view).navigate(id,
            bundle,
            NavOptions.Builder()
                    .setPopUpTo(popStackId,
                            true).build()
    )
}

override fun moveUp(view: View) {
    Navigation.findNavController(view).navigateUp()
}

override fun moveDown(view: View,id: Int) {
    Navigation.findNavController(view).navigate(id)
}

override fun moveDown(view: View,id: Int,args : Bundle) {
    Navigation.findNavController(view).navigate(id,args)
}

fun navigateGraph(graphId : Int,context : Context){
    var finalHost: NavHostFragment?  = NavHostFragment.create(graphId)
    (context as MainActivity).supportFragmentManager.beginTransaction()
            .replace(com.admision.R.id.content,finalHost!!)
            .setPrimaryNavigationFragment(finalHost) // this is the equivalent to app:defaultNavHost="true"
            .commit()
}
}

我找到了一个博客Why I Will Not Use Architecture Navigation Component

1 个答案:

答案 0 :(得分:0)

就像Bhavesh所做的一样,在我的情况下,我只有一个列表,而onCreateView中的这一行对我来说足够了:

if (adapter.isEmpty()) {
            viewModel.getShopCats()
}

和适配器只需要具有以下内容:

fun isEmpty(): Boolean {
        return modelList.isNullOrEmpty()
}