0

i am creating navigation graph in android, and i give args for the destination, but when I called the function, it was saying too many arguments.

  • already setup on build.gradle (app) apply plugin: 'androidx.navigation.safeargs'

  • already setup on build.gradle (.) dependecies { classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0-alpha05" }

  • this is my source of my navigation

<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>
  • this is my destination of my navigation
<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>
  • this is my kotlin code for source of my navigation
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)
    }
}

When I called it inside of unused gameFinished function, GameFragmentDirections.actionGameFragmentDestinationToScoreFragment(score) it was saying to many arguments, any idea how to solve?

ridhopratama
  • 47
  • 1
  • 10
  • Do you have access to the `score` in `ScoreFragment` using the args? – aminography Sep 05 '20 at 12:23
  • can you post the full code where your `sc` is defined and where you define and execute your navigation? – cewaphi Sep 05 '20 at 12:43
  • @cewaphi thank you, done add it on my questions ;) – ridhopratama Sep 05 '20 at 13:01
  • @aminography yes, i have access to it by "val args = ScoreFragmentArgs.fromBundle( arguments ) val score = args.score" but still, Too many arguments in source of my navigation – ridhopratama Sep 05 '20 at 13:01
  • 1
    is this answer your question https://stackoverflow.com/questions/57029081/androidx-navigation-too-many-arguments-for-nonnull-public-open-fun also check [this](https://stackoverflow.com/questions/63058373/why-cant-i-pass-an-argument-to-fragment-using-navigation-component-when-that-ar/63074537#63074537) – Mohammed Alaa Sep 05 '20 at 13:06
  • Go to the source of `actionGameFragmentDestinationToScoreFragment` and check the generated code for it. – aminography Sep 05 '20 at 13:12
  • @MohammedAlaa yeaaa, my question is duplicate, thanks for your response. resolved by removing "android:defaultValue="0"" and rebuild. – ridhopratama Sep 05 '20 at 13:16
  • 1
    @aminography previously it was not receiving any paramater,. But after i remoce " android:defaultValue="0" " in navigation graph and rebuild, now it was receiving one parameters, that is score. thank you, my problem was solved – ridhopratama Sep 05 '20 at 13:17
  • currently i was following tutorial on android kotlin tutorial by udacity It was providing template project, Instead of use it, I was set it up by myself. In their provided template, problem not appearing eventhough "android:defaultValue="0"" is exist. but why in my case and in duplicate issue like @MohammedAlaa mention, that problem appear? – ridhopratama Sep 05 '20 at 13:24
  • check this might clear the point https://stackoverflow.com/questions/60807867/can-i-send-nullable-data-type-as-argument-using-safeargs-if-not-what-should-i/60807951#60807951 – Mohammed Alaa Sep 05 '20 at 15:40

0 Answers0