I have an issue with getBinding. My BaseFragment implementation here:
private var _binding: T? = null
protected val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = inflate(inflater, container, false)
return binding.root
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
The problem, when I realize this scenario:
Fragment 1 -> Fragment 2 -> quickly back to Fragment 1 -> then NullPointerException appears
If I remove _binding = null
in onDestroyView it will be okay, but it opens the possibility of a memory leak.
So my question is why does this exception appear? My binding should initialized every time in onCreateView
UPDT:
Here is full logs with an error:
Process: com.***.***, PID: 23789
java.lang.NullPointerException
at com.android.*.ui.webview.QuoteFragment.getBinding(QuoteFragment.kt:23)
at com.android.*.ui.webview.QuoteFragment.access$getBinding(QuoteFragment.kt:20)
at com.android.*.ui.webview.QuoteFragment$WebClient.onPageStarted(QuoteFragment.kt:109)
at WV.nZ.c(chromium-TrichromeWebViewGoogle6432.aab-stable-599311133:16)
at WV.V6.handleMessage(chromium-TrichromeWebViewGoogle6432.aab-stable-599311133:505)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:210)
at android.os.Looper.loop(Looper.java:299)
at android.app.ActivityThread.main(ActivityThread.java:8293)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:556)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1045)
So what code is throwing the exception? How does that code stop itself when the View is destroyed? Please include that code and the full stack trace.
@ianhanniballake, thanks for your question. I’ve already added logs with an error. Thanks
Can you show us the entire base fragment and an example usage?