Can't Access Fragment Views

img1.jpg

When the observer is still active on the parent's view, the Can't access Fragment Views error occurs. Even if the view is deleted, LifecycleOwner remains ( Navigating to the next Fragment)

Came Across this error while trying to observe live data through the current viewLifecycleOwner

img2.png

SOLUTION:

A possible and very simple workaround for this type of problem is to encapsulate the code that causes it in an if statement with the condition to check if the view is null or not.

img3.png

Wrap Up:

As long as our condition is true, our line of code has a view to which it may connect, making it accessible. However, if not, such a viewpoint becomes disconnected.