1

I made a recyclerview grid that gets filled with data from an api once a user presses a button. After rotating the screen, the scroll position is briefly restored before scrolling back up to the top of the grid. I am using this tutorial to preserve my scroll position:

class MainActivity : AppCompatActivity(), Main.View {
    private lateinit var carAdapter: CarAdapter
    private lateinit var presenter: Main.MainPresenter
    private lateinit var cars: ArrayList<Car?>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        carsAdapter = CarsAdapter(mutableListOf())
        // line suggested by androiddevelopers article
        carsAdapter.stateRestorationPolicy = 
        RecyclerView.Adapter.StateRestorationPolicy.PREVENT_WHEN_EMPTY

        gridRV.layoutManager = GridLayoutManager(this, 3)
        gridRV.adapter = carsAdapter
        if (savedInstanceState != null) {
            cars = savedInstanceState.getParcelableArrayList<Car?>(CARS_ARG)
                    as ArrayList<Car?>
            carsAdapter.cars = cars
            carsAdapter.notifyDataSetChanged()
        }
        presenter = Main.MainPresenter(this, CarService(R.string.api_ul))
        search_button.setOnClickListener { presenter.onSearchClick() } 
    }

    // presenter.onSearchClick() calls this
    override fun displayGrid() {
        this.cars = cars
        carsAdapter.cars = cars
        carsAdapter.notifyDataSetChanged()
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putParcelableArrayList(CARS_ARG, cars)
    }
}

I also tried saving the layout state in this tutorial.

My manifest entry is:

<activity
    android:name=".main.MainActivity"
    android:launchMode="singleTop">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
tohid noori
  • 221
  • 1
  • 11
HukeLau_DABA
  • 2,384
  • 6
  • 33
  • 51

1 Answers1

-1

Well the screen rotation is a configuration change, that causes the activity to recreate and thus you lose the position of scroll.

Although you can save the position and restore.

You can also make your activity ignore the configuration change so that it doesn't recreate. By doing this in manifest:

<activity
    android:name=".main.MainActivity"
    android:configChanges="keyboardHidden|orientation|screenSize"
    android:launchMode="singleTop">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
Hasan Bou Taam
  • 4,017
  • 2
  • 12
  • 22
  • yes it works but documentation strongly advises against it. – HukeLau_DABA May 31 '20 at 13:51
  • Because they think that you want to handle the screen orientation change in your app (maybe by setting a new layout for landscape), if you don't want to handle then I think its okay. – Hasan Bou Taam May 31 '20 at 13:55