如何在Kotlin Android中放置序列化并获取serializeContext? [英] How to put serialize and get serializeContext in Kotlin Android?
本文介绍了如何在Kotlin Android中放置序列化并获取serializeContext?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想保存并保持片段状态,因为当点击recyclerView项并使用侦听器返回片段时,我丢失了上下文,片段中的上下文为 null .
所以我尝试保持这样的上下文:
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)
}
推荐答案
您应正确初始化context
.例如,检查以下内容
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
}
}
}
这篇关于如何在Kotlin Android中放置序列化并获取serializeContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文