Search code examples
androidstaticandroid-savedstate

Difference between Saved Instance State and Static variables


I'm creating an app that downloads some stuff when it starts and show a list to the user, but i don't want it re-downloading the list every time the app opens, or whenever the user changes to a different activity and comes back.

To solve this, i'm using static variables. I read that their values still there until the program is closed or they are not in use and the garbage collector does his work.

So, my question is:

Is there a big difference between these two ways? Or am i doing it wrong? Should i use the Saved Instance State provided by Android to save these variables or static will do the job?

Note that I can not save this date for a time longer then a day, so it's not an option to save it to a database.

Thanks.


Solution

  • Yes, there is a big difference. Even if Android terminates the app, it can keep around the saved state information so that it can be restored at the last place the user left it (e.g. from the recent apps list). With static variables, that data is gone once the process terminates.

    Saved states are really meant for Activity or Fragment specific things (like UI state), not for large amounts of data. If you are downloading data from the web, I suggest you look into persistent data storage options instead of using static variables.