Issue
The thing is: when I started to learn developing for android, I thought I would store my internal datas in public static variables within a dedicated class, for this purpose only, so I could access them from any activity easily, and it was and still is pretty important, that there won't be any duplicate, and theese values are always unique, something like a Singleton model. This is working awesomely, tho it is not the prettiest solution for sure. The problem starts when the user puts the application to the background via home button. It remains there for god knows how much time, maybe minutes, maybe days, or even more...then he reopens the application, and all theese static variables became null, since the system already destroyed them. Some of theese variables are simple Strings, but some are pretty complex, own classes.
The first question is, is there any global hook that I can rely on, where I can save theese datas, when the app goes to the background? So when the user pulls the app to the foreground again, I could just reinitialize theese variables from the saved state.
The second question is, what would be the best way to save theese complex datas? As far as I understand, for saving Strings SharedPreferences are just fine, but what about the more complex classes? Can I use SharedPreferences as well, or should I use something else?
For now this is all I can think of, thx for the help in advance!
Solution
Bad idea with the whole static variables in class. Ignoring the whole global variable debacle, they don't persist. You should use SharedPreferences or a database if you need to persist data.
Answered By - toadzky
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.