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

后端 未结 4 991
遇见更好的自我
遇见更好的自我 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:57

    I have seen passing references in StackOverflow that these might not be safe.

    They are not "safe" insofar as your process will be terminated from time to time, wiping out your static data members (and your custom Application, for that matter). Hence, static data members are good for a cache and not much else.

    Within that scope, they are "safe".

    You just need to make sure that this data is either stored somewhere persistent (e.g., file) or otherwise can be regenerated once the process is terminated and later is started up again. This is no different than with Application.

    However I've tried two different applications using the same class, and even when running both applications side by side on a Galaxy S3 in multi-window mode, the static class variables remain separate.

    Correct. Those are separate processes, with separate copies of your class and objects.

提交回复
热议问题