Skip to content

Commit d1c0c5f

Browse files
Update View Binding
1 parent d45812c commit d1c0c5f

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,21 @@ import guide.graphql.toc.data.Status
1717
import guide.graphql.toc.databinding.ChaptersFragmentBinding
1818

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

2126
private val viewModel: ChaptersViewModel by viewModels()
2227

23-
private lateinit var binding: ChaptersFragmentBinding
2428

2529
override fun onCreateView(
2630
inflater: LayoutInflater,
2731
container: ViewGroup?,
2832
savedInstanceState: Bundle?
2933
): View? {
30-
binding = ChaptersFragmentBinding.inflate(inflater)
34+
_binding = ChaptersFragmentBinding.inflate(inflater, container, false)
3135
return binding.root
3236
}
3337

@@ -86,4 +90,9 @@ class ChaptersFragment : Fragment() {
8690
}
8791
})
8892
}
93+
94+
override fun onDestroyView() {
95+
super.onDestroyView()
96+
_binding = null
97+
}
8998
}

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
@@ -18,15 +18,20 @@ class SectionsFragment : Fragment() {
1818

1919
private val viewModel: SectionsViewModel by viewModels()
2020

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

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

@@ -78,6 +83,11 @@ class SectionsFragment : Fragment() {
7883
viewModel.chapterId = args.chapterId
7984
}
8085

86+
override fun onDestroyView() {
87+
super.onDestroyView()
88+
_binding = null
89+
}
90+
8191
private fun showErrorMessage(error: String) {
8292
binding.spinner.visibility = View.GONE
8393
binding.error.text = error

0 commit comments

Comments
 (0)