androidandroid-fragmentsandroid-recyclerviewadapteronsaveinstancestate

list starts loading from the beginning after screen rotation


In my fragment I have list. This list is is constantly filling. But after screen rotation list starts loading from the beginning. And the data that was in the vertical orientation is lost because in the horizontal orientation the list starts to load again. So, I added all what I could read in the internet: in my Manifest file:

android:configChanges="orientation|screenSize|screenLayout|keyboardHidden"

in Activity class:

if (savedInstanceState == null) {
            firstFragment = FirstFragment()
            supportFragmentManager
                .beginTransaction()
                .add(R.id.frameLayout, firstFragment!!, "firstFragment")
                .commit()
        } else {
            firstFragment = supportFragmentManager
                .findFragmentByTag("firstFragment") as? FirstFragment
        }

but it did not help.


Solution

  • Please set setRetainInstance(true); in fragment code.

    It will prevent recreating fragment instance.