|
1 | 1 | package com.d4rk.androidtutorials.java.ui.screens.quiz; |
2 | 2 |
|
3 | 3 | import android.os.Bundle; |
4 | | -import android.view.MenuItem; |
| 4 | +import android.view.LayoutInflater; |
| 5 | +import android.view.View; |
| 6 | +import android.view.ViewGroup; |
| 7 | +import android.widget.TextView; |
5 | 8 |
|
6 | 9 | import androidx.annotation.NonNull; |
7 | | -import androidx.appcompat.app.ActionBar; |
8 | | -import androidx.appcompat.app.AppCompatActivity; |
| 10 | +import androidx.annotation.Nullable; |
| 11 | +import androidx.fragment.app.Fragment; |
9 | 12 | import androidx.lifecycle.ViewModelProvider; |
| 13 | +import androidx.navigation.fragment.NavHostFragment; |
10 | 14 |
|
| 15 | +import com.airbnb.lottie.LottieAnimationView; |
11 | 16 | import com.d4rk.androidtutorials.java.R; |
12 | 17 | import com.d4rk.androidtutorials.java.data.model.QuizQuestion; |
13 | | -import com.d4rk.androidtutorials.java.databinding.ActivityQuizBinding; |
| 18 | +import com.d4rk.androidtutorials.java.databinding.FragmentQuizBinding; |
14 | 19 | import com.d4rk.androidtutorials.java.utils.EdgeToEdgeDelegate; |
15 | 20 | import com.google.android.material.dialog.MaterialAlertDialogBuilder; |
16 | | -import android.view.LayoutInflater; |
17 | | -import android.view.View; |
18 | | -import android.widget.TextView; |
19 | | -import com.airbnb.lottie.LottieAnimationView; |
20 | 21 |
|
21 | | -/** |
22 | | - * Activity that displays a simple multiple-choice quiz. |
23 | | - */ |
24 | 22 | import dagger.hilt.android.AndroidEntryPoint; |
25 | 23 |
|
26 | 24 | @AndroidEntryPoint |
27 | | -public class QuizActivity extends AppCompatActivity { |
| 25 | +public class QuizFragment extends Fragment { |
28 | 26 |
|
29 | | - private ActivityQuizBinding binding; |
| 27 | + private FragmentQuizBinding binding; |
30 | 28 | private QuizViewModel viewModel; |
31 | 29 |
|
| 30 | + @Nullable |
32 | 31 | @Override |
33 | | - protected void onCreate(Bundle savedInstanceState) { |
34 | | - super.onCreate(savedInstanceState); |
35 | | - binding = ActivityQuizBinding.inflate(getLayoutInflater()); |
36 | | - setContentView(binding.getRoot()); |
| 32 | + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, |
| 33 | + @Nullable Bundle savedInstanceState) { |
| 34 | + binding = FragmentQuizBinding.inflate(inflater, container, false); |
37 | 35 |
|
38 | | - EdgeToEdgeDelegate edgeToEdgeDelegate = new EdgeToEdgeDelegate(this); |
| 36 | + EdgeToEdgeDelegate edgeToEdgeDelegate = new EdgeToEdgeDelegate(requireActivity()); |
39 | 37 | edgeToEdgeDelegate.applyEdgeToEdge(binding.container); |
40 | 38 |
|
41 | | - ActionBar actionBar = getSupportActionBar(); |
42 | | - if (actionBar != null) { |
43 | | - actionBar.setDisplayHomeAsUpEnabled(true); |
44 | | - } |
45 | | - |
46 | 39 | viewModel = new ViewModelProvider(this).get(QuizViewModel.class); |
47 | 40 | if (viewModel.getTotalQuestions() == 0) { |
48 | | - new MaterialAlertDialogBuilder(this) |
| 41 | + new MaterialAlertDialogBuilder(requireContext()) |
49 | 42 | .setMessage(R.string.quiz_no_more_questions) |
50 | | - .setPositiveButton(android.R.string.ok, (d, w) -> finish()) |
| 43 | + .setPositiveButton(android.R.string.ok, (d, w) -> NavHostFragment.findNavController(this).popBackStack()) |
51 | 44 | .setCancelable(false) |
52 | 45 | .show(); |
53 | | - return; |
| 46 | + } else { |
| 47 | + showQuestion(viewModel.getCurrentQuestion()); |
| 48 | + binding.buttonNext.setOnClickListener(v -> onNextClicked()); |
54 | 49 | } |
55 | | - showQuestion(viewModel.getCurrentQuestion()); |
56 | 50 |
|
57 | | - binding.buttonNext.setOnClickListener(v -> onNextClicked()); |
| 51 | + return binding.getRoot(); |
58 | 52 | } |
59 | 53 |
|
60 | 54 | private void onNextClicked() { |
@@ -94,24 +88,21 @@ private void showQuestion(QuizQuestion question) { |
94 | 88 | private void showResult() { |
95 | 89 | int score = viewModel.getScore().getValue(); |
96 | 90 | int total = viewModel.getTotalQuestions(); |
97 | | - View view = LayoutInflater.from(this).inflate(R.layout.dialog_quiz_result, null, false); |
| 91 | + View view = LayoutInflater.from(requireContext()).inflate(R.layout.dialog_quiz_result, null, false); |
98 | 92 | TextView textResult = view.findViewById(R.id.text_result); |
99 | 93 | textResult.setText(getString(R.string.quiz_finished, score, total)); |
100 | 94 | LottieAnimationView animationView = view.findViewById(R.id.animation_success); |
101 | 95 | animationView.playAnimation(); |
102 | | - new MaterialAlertDialogBuilder(this) |
| 96 | + new MaterialAlertDialogBuilder(requireContext()) |
103 | 97 | .setView(view) |
104 | | - .setPositiveButton(android.R.string.ok, (d, w) -> finish()) |
| 98 | + .setPositiveButton(android.R.string.ok, (d, w) -> NavHostFragment.findNavController(this).popBackStack()) |
105 | 99 | .setCancelable(false) |
106 | 100 | .show(); |
107 | 101 | } |
108 | 102 |
|
109 | 103 | @Override |
110 | | - public boolean onOptionsItemSelected(@NonNull MenuItem item) { |
111 | | - if (item.getItemId() == android.R.id.home) { |
112 | | - finish(); |
113 | | - return true; |
114 | | - } |
115 | | - return super.onOptionsItemSelected(item); |
| 104 | + public void onDestroyView() { |
| 105 | + super.onDestroyView(); |
| 106 | + binding = null; |
116 | 107 | } |
117 | 108 | } |
0 commit comments