Is it safe to use static class variables in an android application

后端 未结 4 1012
遇见更好的自我
遇见更好的自我 2021-01-19 01:52

I am aware of the technique of extending the Application class to provide global storage. However in my case I am writing a class for a library function, so do not wish to f

4条回答
  •  情深已故
    2021-01-19 02:39

    If your goal is to store persistant data across the twists and turns of the application lifecycle, then I would recommend not using static variables to do so. The obvious problem with this approach is that they could easily be garbage collected by the system when the operating system decides to reclaim memory (i.e. when the screen sleeps or a different application starts a memory-intensive task). I'm not sure what kind of data you are looking to "store", but I would recommend saving the state in SharedPreferences or an SQLiteDatabase instead.

提交回复
热议问题