I am using motion layout in my mainactivity. It is working proplerly. However when I move to other activities and navigate back to my mainactivity sometimes the activity is
What I did was adding a field, a boolean field to be specific and use the life cycle to handle it.
private var hasMotionScrolled = false
override fun onResume() {
super.onResume()
if (hasMotionScrolled) motionLayout.progress = MOTION_TRANSITION_COMPLETED
}
override fun onPause() {
super.onPause()
hasMotionScrolled = motionLayout.progress > MOTION_TRANSITION_INITIAL
}
companion object {
private const val MOTION_TRANSITION_COMPLETED = 1F
private const val MOTION_TRANSITION_INITIAL = 0F
}
So in my case, motion layout is doing an animation related to scrolling. If this is not your case maybe you can use directly the motionLayout.progress
. My problem with using the progress
directly was that intermediate states would make other elements not visible when navigating back, that is why implemented an all or nothing boolean flag.
I don't see this as a clean solution, a flag is always something that means something else could have been better, if you can find something official, please let me know in the comments.