Skip to content

Commit a730e97

Browse files
Update View Binding for fragments
1 parent bcf8565 commit a730e97

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

app/src/main/java/guide/graphql/toc/ui/chapters/ChaptersFragment.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,18 @@ import guide.graphql.toc.data.apolloClient
1919
import guide.graphql.toc.databinding.ChaptersFragmentBinding
2020

2121
class ChaptersFragment : Fragment() {
22+
private var _binding: ChaptersFragmentBinding? = null
23+
// This property is only valid between onCreateView and
24+
// onDestroyView.
25+
private val binding get() = _binding!!
26+
2227

23-
private lateinit var binding: ChaptersFragmentBinding
2428
override fun onCreateView(
2529
inflater: LayoutInflater,
2630
container: ViewGroup?,
2731
savedInstanceState: Bundle?
2832
): View? {
29-
binding = ChaptersFragmentBinding.inflate(inflater)
33+
_binding = ChaptersFragmentBinding.inflate(inflater, container, false)
3034
return binding.root
3135
}
3236

@@ -85,6 +89,10 @@ class ChaptersFragment : Fragment() {
8589
}
8690
}
8791

92+
override fun onDestroyView() {
93+
super.onDestroyView()
94+
_binding = null
95+
}
8896

8997
private fun showErrorMessage(errorMessage: String) {
9098
Toast.makeText(

app/src/main/java/guide/graphql/toc/ui/sections/SectionsFragment.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,20 @@ import guide.graphql.toc.databinding.SectionsFragmentBinding
1919
class SectionsFragment : Fragment() {
2020

2121

22-
private lateinit var binding: SectionsFragmentBinding
22+
private var _binding: SectionsFragmentBinding? = null
23+
// This property is only valid between onCreateView and
24+
// onDestroyView.
25+
private val binding get() = _binding!!
26+
27+
2328
private val args: SectionsFragmentArgs by navArgs()
2429

2530
override fun onCreateView(
2631
inflater: LayoutInflater,
2732
container: ViewGroup?,
2833
savedInstanceState: Bundle?
2934
): View? {
30-
binding = SectionsFragmentBinding.inflate(inflater)
35+
_binding = SectionsFragmentBinding.inflate(inflater, container, false)
3136
return binding.root
3237
}
3338

@@ -86,6 +91,11 @@ class SectionsFragment : Fragment() {
8691
}
8792
}
8893

94+
override fun onDestroyView() {
95+
super.onDestroyView()
96+
_binding = null
97+
}
98+
8999
private fun showErrorMessage(error: String) {
90100
binding.spinner.visibility = View.GONE
91101
binding.error.text = error

0 commit comments

Comments
 (0)