0

我在 android 中创建导航图,并为目的地提供 args,但是当我调用该函数时,它说的参数太多。

  • 已经在 build.gradle (app) 上设置apply plugin: 'androidx.navigation.safeargs'

  • 已经在 build.gradle (.) dependecies { classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0-alpha05" }

  • 这是我的导航来源

<fragment
        android:id="@+id/game_fragment_destination"
        android:name="com.example.apparchitectureuilayer.screen.game.GameFragment"
        android:label="GameFragment"
        tools:layout="@layout/fragment_game">
        <action
            android:id="@+id/action_game_fragment_destination_to_scoreFragment"
            app:destination="@id/score_fragment"
            app:launchSingleTop="true"
            app:popUpTo="@+id/title_destination" />
    </fragment>
  • 这是我导航的目的地
<fragment
        android:id="@+id/score_fragment"
        android:name="com.example.apparchitectureuilayer.screen.score.ScoreFragment"
        android:label="ScoreFragment"
        tools:layout="@layout/fragment_score">
        <action
            android:id="@+id/action_score_fragment_to_game_fragment_destination"
            app:destination="@id/game_fragment_destination" />
        <argument
            android:name="score"
            android:defaultValue="0"
            app:argType="integer" />
    </fragment>
  • 这是我的导航源的 kotlin 代码
class GameFragment : Fragment() {

    private var word = ""
    private var score = 0
    private lateinit var wordList: MutableList<String>

    private lateinit var binding: FragmentGameBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_game, container, false)
    }

    private fun gameFinished() {
        val action = GameFragmentDirections.actionGameFragmentDestinationToScoreFragment(score)
        NavHostFragment.findNavController(this).navigate(action)
    }
}

当我在未使用的 gameFinished 函数中调用它时,GameFragmentDirections.actionGameFragmentDestinationToScoreFragment(score) 它对很多参数说,知道如何解决吗?

4

0 回答 0