Motion Layout reset on navigating between activities

前端 未结 4 775
鱼传尺愫
鱼传尺愫 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 04:54
    class ExtMotionLayout : MotionLayout {
        constructor(context: Context) : super(context)
        constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
        constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
    
        override fun onSaveInstanceState(): Parcelable? {
            return SavedState(progress, super.onSaveInstanceState())
        }
    
        override fun onRestoreInstanceState(state: Parcelable?) {
            if (state is SavedState) {
                progress = state.progress
                super.onRestoreInstanceState(state.superState)
            } else super.onRestoreInstanceState(state)
        }
    
        class SavedState : BaseSavedState {
            val progress: Float
    
            constructor(progress: Float, source: Parcelable?) : super(source) {
                this.progress = progress
            }
    
            constructor(superState: Parcel) : super(superState) {
                progress = superState.readFloat()
            }
    
            override fun writeToParcel(out: Parcel, flags: Int) {
                super.writeToParcel(out, flags)
                out.writeFloat(progress)
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-19 04:55

    You can add a transition listener to the motion layout & save a flag when the transition is completed. Afterwards, when the activity gets recreated, you can read that flag and use smth like:

    motionLayout.setState(R.id.end, ConstraintSet.WRAP_CONTENT, ConstraintSet.WRAP_CONTENT)

    Where R.id.end is the id from constraintSetEnd property.

    0 讨论(0)
  • 2021-01-19 04:56

    You have to save/restore the progress of your MotionLayout:

        override fun onSaveInstanceState(outState: Bundle) {
            super.onSaveInstanceState(outState)
            outState.putFloat("progress", motionLayout.progress)
        }
    
        override fun onCreate(savedInstanceState: Bundle?) {
            ...
            if (savedInstanceState != null)
                motionLayout.progress = savedInstanceState.getFloat("progress", 0f)
            ...
        }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题