gpt4 book ai didi

android - 架构导航组件 : onCreateView gets called every time

转载 作者:行者123 更新时间:2023-12-04 12:50:34 27 4
gpt4 key购买 nike

每次按下返回时都会创建 fragment 实例并重新加载。

如何克服这个问题?

无法在导航 Controller 中拥有适当的后台堆栈是一个巨大的生产力问题。希望这是一个缺失的功能或故意的变通方法... IMO 这是必须具备的,必须尽早引入(但是:https://issuetracker.google.com/issues/109856764 表示我们不会允许自定义交易类型(例如隐藏/显示等)在可预见的 future 。):(

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

有没有人有解决方案?

我的代码是使用 Navigation 进行导航

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

最佳答案

我确实喜欢这个,但我仍在等待谷歌的解决方案。

在这里我们做到了,但仍在等待更好的方法

if (manageEventViewModel == null) {
manageEventViewModel = ViewModelProviders.of(activity!!).get(ManageEventViewModel::class.java)
manageEventViewModel!!.setBinder(binding!!,context!!,listingType)
loadAdvertisement(binding!!.advertise.imgAdvertisement,binding!!.advertise.tvAdvertisement,binding!!.advertise.llAdvertisement)
}

完整代码
class ManageEventsFragment : BaseFragment() {

private var binding: FragmentFindEventsBinding? = null
private var manageEventViewModel: ManageEventViewModel? = null
override fun onCreateView(inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?): View? {
if (binding == null)
binding = DataBindingUtil.inflate(inflater,R.layout.fragment_find_events,container,false)


var listingType: String? = null
if (arguments != null) {
listingType = arguments!!.getString(Constant.LISTINGTYPE)
/*if (listingType != null && listingType.equals(Constant.SEARCHLIST)) {
val data = arguments!!.getString(Constant.SEARCHEVENTMODEL)
val managetEventDataModel = com.admision.manageevents.utils.Utils.getManageEventDataModel(data)
manageEventViewModel!!.setSearchDataModel(managetEventDataModel!!)
}*/
}

val sharedViewModel = ViewModelProviders.of(activity!!).get(SharedViewModel::class.java)
sharedViewModel!!.dataToShare.observe(this,Observer<ManageEventsDataModel> { managetEventDataModel ->
listingType = Constant.SEARCHLIST
manageEventViewModel!!.setSearchDataModel(managetEventDataModel!!)
})

if (manageEventViewModel == null) {
manageEventViewModel = ViewModelProviders.of(activity!!).get(ManageEventViewModel::class.java)
manageEventViewModel!!.setBinder(binding!!,context!!,listingType)
loadAdvertisement(binding!!.advertise.imgAdvertisement,binding!!.advertise.tvAdvertisement,binding!!.advertise.llAdvertisement)
}
return binding!!.root
}
}

关于android - 架构导航组件 : onCreateView gets called every time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54879581/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com