问题
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