ViewBinding Null Exception Error in Fragment after reopen the fragment

Viewed 862

I want to ask something. I used viewbinding inside fragment and navigation component to navigate fragment.

This is the case

(First Task) From Activity A (There is no fragment) -> Activity B (Fragment A) and do some process. after that i finish Activity B.

And then now I'm in Activity A. The problem is, when I do same task like that

(Second Task) Activity A -> Activity B (Fragment A) and do some process in fragment A, i got null view binding.

Can you give me solution for this error? i can't solve it Thanks before

Here my code in Fragment A

private var _binding : FragmentOrderBinding? = null
private val binding get() = _binding!!

.....

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    _binding = FragmentOrderBinding.inflate(inflater, container, false)
    val mapsView = binding?.root?.findViewById<ViewGroup>(R.id.maps_view)
    markerViewAdapter = MarkerViewAdapter(mapsView!!)
    return binding?.root
}

......

override fun onDestroyView() {
    super.onDestroyView()
    _binding = null
}
2 Answers

Try move markerViewAdapter into onViewCreate.

Or use view binding delegate library for handle view binding, many available on github.

Related