1
0
Форкнуть 0

19.Exercise: Adding Safe Arguments

This commit is contained in:
Diego Ezequiel Guillén 2020-08-05 16:49:58 -03:00
Родитель 619389b6c5
Коммит d763cf3015
7 изменённых файлов: 35 добавлений и 10 удалений

Просмотреть файл

@ -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