Motion Layout reset on navigating between activities

前端 未结 4 804
鱼传尺愫
鱼传尺愫 2021-01-19 04:23

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

4条回答
  •  春和景丽
    2021-01-19 05:05

    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.

提交回复
热议问题