android-motionlayout

Motion Layout reset on navigating between activities


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 reset and the layout is in its starting state.How do I keep this from happening ? Apart from this I also have another question about motion layout which I have asked here


Solution

  • class CustomMotionLayout(context: Context, attrs: AttributeSet) : MotionLayout(context, attrs) {
        init {
            isSaveEnabled = true
        }
    
        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(val progress: Float, source: Parcelable?) : BaseSavedState(source) {
    
            override fun writeToParcel(out: Parcel, flags: Int) {
                super.writeToParcel(out, flags)
                out.writeFloat(progress)
            }
        }
    }
    

    Use CustomMotionLayout instead of default MotionLayout Just keep in mind to provide id to your motion layout