androidandroid-asynctaskthemesrecreate

how to prevent loss of data on calling recreate activity


I have to change Theme of activity when users feels like changing it. For that,I have to call recreate() after setting theme to activate the Theme. But my Activity gets data from web and displays to user in a Recycler view. every time the theme changes,The data is being refreshed, I need to prevent this.

Is there any possible way to save data when calling recreate() other than saving in a database ?

I tried overriding onSaveInstanceState(); but savedInstanceState in onCreate() seems to be 'null' when recreate is called.


Solution

  • You should save your data in a File such as a SQLite DataBase or SharedPreferences

    Life-Cycle of Activity will change many times and you can't prevent this, your data should be saved and retrieved on an Event like OnStart or OnResume

    As another suggestion don't use something like Static-Variables to save your Data, Static Variables stored in the Heap and will be cleared when your Activity is not in use and Android OS needs more Memory