在活动之间导航时重置运动布局 [英] Motion Layout reset on navigating between activities

查看:66
本文介绍了在活动之间导航时重置运动布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在mainactivity中使用动作布局。它工作正常。但是,当我移到其他活动并导航回我的mainactivity时,有时会重置该活动并且布局处于开始状态。如何防止这种情况发生?除了这笔
之外,我还有一个关于动作布局的问题,我已经问过这里

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

推荐答案

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)
        }
    }
}

这篇关于在活动之间导航时重置运动布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆