在我的应用程序中,我有BottomNavBar
,并且我想在单击该fragments
的项目时显示BottomNavBar
!
为了使用fragments
设置此BottomNavBar
,我使用了 NavigationGraph
组件!
我想为此setUserVisibleHint
之一使用ojit_strong fragment
方法,但是当显示此fragment
时,不要调用 setUserVisibleHint
!
我的 Activity 代码,用于将fragments
设置为BottomNavBar
项目:
class HomeActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setupNavigation()
}
private fun setupNavigation() {
val navController = Navigation.findNavController(this, R.id.homePage_fragmentNavHost)
NavigationUI.setupWithNavController(homePage_bottomNavBar, navController)
}
override fun onSupportNavigateUp() = Navigation.findNavController(this, R.id.homePage_fragmentNavHost).navigateUp()
}
我的片段代码:
class HomeDashboardFragment : Fragment(){
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home_dashboard, container, false)
}
override fun setUserVisibleHint(isVisibleToUser: Boolean) {
super.setUserVisibleHint(isVisibleToUser)
if (isVisibleToUser) {
Handler().postDelayed({ requireContext().toast("Show") }, 500)
}
}
}
为什么不将
setUserVisibleHint
转换为fragment
? 最佳答案
在您放置此代码之前,您已了解片段生命周期
override fun setUserVisibleHint(isVisibleToUser: Boolean) {
super.setUserVisibleHint(isVisibleToUser)
if (isVisibleToUser) {
// post your code
}
}
override fun onStart() {
super.onStart()
userVisibleHint = true
}