How to put serialize and get serializeContext in Kotlin Android?

巧了我就是萌 提交于 2020-02-08 10:06:33

问题


I want save and keep my state in fragment because when tap on recyclerView items and back to fragment with listener i lost my Context and my context in fragment is null.
So i try to keep my context like this:

override fun onSaveInstanceState(outState: Bundle) {
    super.onSaveInstanceState(outState)
    outState.putSerializable("SaveContext", context)
}

回答1:


You should initialize your context correctly. Check below as for example

class MyFragment: Fragment() {

    private var mContext: Context? = null

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mContext = context
    }

    override fun onDetach() {
        super.onDetach()
        mContext = null
    }

    fun callbackListener() {
        mContext?.let { 
            //do your operation
        }
    }
}


来源:https://stackoverflow.com/questions/59713626/how-to-put-serialize-and-get-serializecontext-in-kotlin-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!