0

i'm trying to develop an app thats reading excel file(.xlsx) to a ListView and i noticed that every time the rotation changes or if i change to night mode (or the opposite) the list view resets and starts to load the file again,

i know it related somehow to the lifecycle methods (onCreate, onDestroy...) and theres an option to save values with sheredPrefences but i can't save arrayAdapter or arrayList to sheredPrefences so what should i do to fix this?

(sorry for my english...)

J El
  • 100
  • 7
  • 1
    I'd suggest to use a pattern like MVP or MVVM to decouple the UI from the data. It's gonna solve your problem as you could reuse the list, to pass it to the new adapter recreated after the Activity is destroyed on the config change. And btw, prefers RecyclerView to ListView. ListView are a now very old way to display lists. And please, please, please, don't store heavy data into the SharedPreferences, they are not meant for that. Especially as you have the data stored already since you're reading from a local Excel file. – Eselfar Nov 18 '20 at 05:00

2 Answers2

1

Have you tried specifying configChanges?

<activity name= ".YourActivity" android:configChanges="orientation|screenSize"/>

This should save your current UI state, before changing the orientation.

However, changing the theme might recreate your activity anyway.

This might be relvant to you

Abu Sufian
  • 41
  • 3
  • it worked for the rotation problem, thank you! but still didn't fix the problem while changing the device to dark mode (android Q, OneUi) – J El Nov 17 '20 at 23:53
0

When you rotate the device, the activity is destroyed and when the rotation is finished, a new instance of activity is created, so if you want to have the previous state, then is necessary to use the onSaveInstanceState(Bundle savedInstanceState) to save your data in the bundle and fetch the data from onRestoreInstanceState(Bundle savedInstanceState)

Manuel Mato
  • 771
  • 4
  • 10