我正在尝试将信息从片段发送到主要活动。
我正在尝试var interfaceName
从主要活动中设置一个片段。
我创建var menuInterface: MenuInterface
并尝试将其设置为onNavigationItemSelected
使用myFragment.menuInterface = this
该menuInterface
停留空出于某种原因...任何想法,为什么?
这 onNavigationItemSelected
override fun onNavigationItemSelected(menuItem: MenuItem): Boolean {
when (menuItem.itemId) {
R.id.feedLayoutId -> {
feedFragment = FeedFragment()
feedFragment.menuInterface = this
barTitle.text = "myTitle"
supportFragmentManager
.beginTransaction()
.replace(R.id.frame_layout, feedFragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit()
}
}
drawerLayout.closeDrawer(GravityCompat.START)
return true
}
在您的活动中实现 MenuInterface,并从您的代码中删除此行。
feedFragment.menuInterface = this
在你的片段中:
private var menuInterface: MenuInterface? = null
override fun onAttach(context: Context?) {
super.onAttach(context)
menuInterface = context as MenuInterface
}
override fun onDetach() {
menuInterface = null
super.onDetach()
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句