英文:
If I pass fragment object throw function is clean code
问题
例如:
object ThisClassForNavigateToSomeFragment {
internal fun navigateToFragment1(
productId: Long, navigateId: Int, fragment: Fragment
) {
val bundle = Bundle()
bundle.putLong("product_id", productId)
fragment.findNavController().navigate(navigateId, bundle)
}
internal fun navigateToFragment(
productId: Long, navigateId: Int, fragment: Fragment
) {
val bundle = Bundle()
bundle.putLong("product_id", productId)
fragment.findNavController().navigate(navigateId, bundle)
}
}
这是干净的代码吗?如果我通过一个函数传递 fragment,这样做好吗?
英文:
For example:
object ThisClassForNavigateToSomeFragment{
internal fun navigateToFragment1(
productId: Long, navigateId:Int, fragment:Fragment) {
val bundle = Bundle()
bundle.putLong("product_id", productId)
fragment.findNavController().navigate(navigateId, bundle)
}}
internal fun navigateToFragment(
productId: Long, navigateId:Int, fragment:Fragment) {
val bundle = Bundle()
bundle.putLong("product_id", productId)
fragment.findNavController().navigate(navigateId, bundle)
}}
Is this clean code, is this good if I pass fragment throw a function?
答案1
得分: 0
为什么要为导航使用不同的函数?您可以在BaseFragment或BaseActivity中简单地添加一个方法,如下所示。 (然后在片段或活动中扩展此方法)
protected fun navigate(directions: NavDirections) {
findNavController().navigate(directions)
}
这很简单,您实际上不需要一个对象来执行这个简单的任务。
由于您传递了NavDirection对象,您可以选择传递带有额外信息或不带额外信息的对象。
英文:
Why do you want to have different functions for navigation? You can simply add a method in BaseFragment or BaseActivity like the following. ( And extend this in your fragments or activity)
protected fun navigate(directions: NavDirections) {
findNavController().navigate(directions)
}
This is simple, and you really don't need an object to do this simple task.
Since you pass NavDirection object, you can either pass one with or without extras.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论