如果我通过函数传递片段对象是干净的代码。

huangapple go评论74阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年7月27日 21:01:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63115901.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定