19.Exercise: Adding Safe Arguments
This commit is contained in:
Родитель
619389b6c5
Коммит
d763cf3015
|
@ -20,6 +20,8 @@ apply plugin: 'kotlin-android'
|
|||
|
||||
apply plugin: 'kotlin-kapt'
|
||||
|
||||
apply plugin: 'androidx.navigation.safeargs'
|
||||
|
||||
android {
|
||||
compileSdkVersion 28
|
||||
dataBinding {
|
||||
|
|
|
@ -99,12 +99,15 @@ class GameFragment : Fragment() {
|
|||
} else {
|
||||
// We've won! Navigate to the gameWonFragment.
|
||||
view.findNavController().
|
||||
navigate(R.id.action_gameFragment_to_gameWonFragment)
|
||||
navigate(GameFragmentDirections.actionGameFragmentToGameWonFragment(
|
||||
numQuestions,questionIndex
|
||||
)
|
||||
)
|
||||
}
|
||||
} else {
|
||||
// Game over! A wrong answer sends us to the gameOverFragment.
|
||||
view.findNavController().
|
||||
navigate(R.id.action_gameFragment_to_gameOverFragment)
|
||||
navigate(GameFragmentDirections.actionGameFragmentToGameOverFragment())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,7 +33,9 @@ class GameOverFragment : Fragment() {
|
|||
inflater, R.layout.fragment_game_over, container, false)
|
||||
|
||||
binding.tryAgainButton.setOnClickListener { view: View ->
|
||||
view.findNavController().navigate(R.id.action_gameOverFragment_to_gameFragment)
|
||||
view.findNavController().navigate(
|
||||
GameOverFragmentDirections.actionGameOverFragmentToGameFragment()
|
||||
)
|
||||
}
|
||||
|
||||
return binding.root
|
||||
|
|
|
@ -16,10 +16,11 @@
|
|||
|
||||
package com.example.android.navigation
|
||||
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.view.*
|
||||
import android.widget.Toast
|
||||
import androidx.core.app.ShareCompat
|
||||
import androidx.databinding.DataBindingUtil
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.navigation.findNavController
|
||||
|
@ -34,9 +35,16 @@ class GameWonFragment : Fragment() {
|
|||
inflater, R.layout.fragment_game_won, container, false)
|
||||
|
||||
binding.nextMatchButton.setOnClickListener { view: View ->
|
||||
view.findNavController().navigate(R.id.action_gameWonFragment_to_gameFragment)
|
||||
view.findNavController().navigate(
|
||||
GameWonFragmentDirections.actionGameWonFragmentToGameFragment())
|
||||
}
|
||||
|
||||
var args = GameWonFragmentArgs.fromBundle(arguments!!)
|
||||
Toast.makeText(context,
|
||||
"NumCorrect: ${args.numCorrect}, NumQuestions: ${args.numQuestions}",
|
||||
Toast.LENGTH_LONG).show()
|
||||
|
||||
setHasOptionsMenu(true)
|
||||
return binding.root
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,9 +21,12 @@ class TitleFragment : Fragment() {
|
|||
//view.findNavController().navigate(R.id.action_titleFragment_to_gameFragment)
|
||||
//}
|
||||
//or
|
||||
binding.playButton.setOnClickListener(
|
||||
Navigation.createNavigateOnClickListener(R.id.action_titleFragment_to_gameFragment)
|
||||
)
|
||||
// binding.playButton.setOnClickListener(
|
||||
// Navigation.createNavigateOnClickListener(R.id.action_titleFragment_to_gameFragment)
|
||||
// )
|
||||
binding.playButton.setOnClickListener { v: View ->
|
||||
v.findNavController().navigate(TitleFragmentDirections.actionTitleFragmentToGameFragment())
|
||||
}
|
||||
|
||||
setHasOptionsMenu(true)
|
||||
|
||||
|
|
|
@ -48,6 +48,12 @@
|
|||
android:id="@+id/action_gameWonFragment_to_gameFragment"
|
||||
app:destination="@id/gameFragment"
|
||||
app:popUpTo="@id/titleFragment" />
|
||||
<argument
|
||||
android:name="numQuestions"
|
||||
app:argType="integer" />
|
||||
<argument
|
||||
android:name="numCorrect"
|
||||
app:argType="integer" />
|
||||
</fragment>
|
||||
<fragment
|
||||
android:id="@+id/aboutFragment"
|
||||
|
|
|
@ -31,6 +31,7 @@ buildscript {
|
|||
dependencies {
|
||||
classpath 'com.android.tools.build:gradle:3.3.0'
|
||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$version_kotlin"
|
||||
classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:$version_navigation"
|
||||
|
||||
// NOTE: Do not place your application dependencies here; they belong
|
||||
// in the individual module build.gradle files
|
||||
|
|
Загрузка…
Ссылка в новой задаче