I know I come a bit late to the party, but this may help someone in the future!
I had a similar problem with the Motion layout and all solutions I found resulted in rendering problems when scrolling my motion layout again, especially for an image that disappears on scroll, so I came up with the following for a Fragment
Save the progress of the MotionLayout inside a viewModel:
@HiltViewModel
class OverviewViewModel @Inject constructor() : ViewModel() {
...
var motionProgress = 0.0f
...
}
In the fragments onPause save the progress state of the MotionLayout:
override fun onPause() {
super.onPause()
viewModel.motionProgress = motionLayout.progress
}
And in your onCreateView() method you set the progress to your layout:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
...
motionLayout.post{
motionLayout.progress = viewModel.motionProgress
}
...
}
For anyone wondering, I have tried saving a local variable in onSaveInstanceState() but I had problems with this method not being called when using NavigationComponents
Hope it helps, Cheers!