Task: Add conditional navigation

This commit is contained in:
skysamara 2020-02-19 17:38:15 +03:00
parent 8d1891dc0d
commit aa3d64ff29
2 changed files with 21 additions and 3 deletions

View File

@ -23,6 +23,7 @@ import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.navigation.findNavController
import com.example.android.navigation.databinding.FragmentGameBinding
class GameFragment : Fragment() {
@ -98,10 +99,10 @@ class GameFragment : Fragment() {
setQuestion()
binding.invalidateAll()
} else {
// We've won! Navigate to the gameWonFragment.
view.findNavController().navigate(R.id.action_gameFragment_to_gameWonFragment)
}
} else {
// Game over! A wrong answer sends us to the gameOverFragment.
view.findNavController().navigate(R.id.action_gameFragment_to_gameOverFragment)
}
}
}

View File

@ -18,5 +18,22 @@
android:id="@+id/gameFragment"
android:name="com.example.android.navigation.GameFragment"
android:label="GameFragment"
tools:layout="@layout/fragment_game"/>
tools:layout="@layout/fragment_game">
<action
android:id="@+id/action_gameFragment_to_gameOverFragment"
app:destination="@id/gameOverFragment" />
<action
android:id="@+id/action_gameFragment_to_gameWonFragment"
app:destination="@id/gameWonFragment" />
</fragment>
<fragment
android:id="@+id/gameOverFragment"
android:name="com.example.android.navigation.GameOverFragment"
android:label="fragment_game_over"
tools:layout="@layout/fragment_game_over" />
<fragment
android:id="@+id/gameWonFragment"
android:name="com.example.android.navigation.GameWonFragment"
android:label="fragment_game_won"
tools:layout="@layout/fragment_game_won" />
</navigation>