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