Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

When using LiveData in fragments with bottomNavigation and NavigationController, state loss can occur when the user navigates away from a fragment using bottomNavigation and then returns to that fragment using the back button or the NavigationController.

LiveData objects typically store data that should be retained across configuration changes, such as screen rotations. However, when a user navigates away from a fragment using bottomNavigation, the fragment is destroyed and a new instance of the fragment is created when the user returns to it. This means that the old LiveData object is destroyed along with the fragment, and a new LiveData object is created when the new fragment is instantiated.

If the data in the LiveData object was not saved before the fragment was destroyed, it will be lost when the new LiveData object is created. This can result in the fragment appearing to be in an incorrect or outdated state when the user returns to it, which can cause confusion or frustration.

To avoid state loss when using LiveData in conjunction with bottomNavigation and NavigationController, it is important to save any data that is stored in the LiveData object before the fragment is destroyed, and to restore that data when the new instance of the fragment is created. This can be done using the onSaveInstanceState() and onViewStateRestored() methods in the fragment, which allow you to store and retrieve data across configuration changes.