class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
supportFragmentManager.beginTransaction()
.replace(R.id.fragment_container, MoviesFragment())
.commit()
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putParcelable(
"StoreRecyclerView",
recycler_view.layoutManager?.onSaveInstanceState()
)
Log.d("State", "Save an instance state")
}
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
mLayoutManagerState = savedInstanceState.getParcelable("StoreRecyclerView")
super.onRestoreInstanceState(savedInstanceState)
Log.d("State", "Restore an instance state")
}
override fun onResume() {
super.onResume()
if (mLayoutManagerState != null) {
recycler_view.layoutManager?.onRestoreInstanceState(mLayoutManagerState)
}
Log.d("State", "!!!Restoring an instance state!!!")
}
companion object {
var mLayoutManagerState: Parcelable? = null
}
}
When I turn my phone it can save the state of application and set the position of recyclerView after. But it doesn't. What can be the problem?
All methods are in fragment. But, as I understand, it doesn't matter for save the recyclerView's instanse.