Android : Static variable null on low memory

前端 未结 7 2291
轮回少年
轮回少年 2020-11-29 02:44

I have an application which has some static variables. These variables are stored in an independent Class named DataContext. These variables are initialized from raw files a

相关标签:
7条回答
  • 2020-11-29 03:12

    If you weren't using raw files, I'd advise initializing when the class is loaded.

    For instance,

    public static Map<?,?> myStaticMap = new HashMap<?,?>();
    static { //fill myStaticMap }
    

    You do have some bigger concerns to worry about if you are loading files that way. For instance, what about I/O errors, or latency issues? You will get warnings in gingerbread (if you enable them) for doing I/O in your main thread. Perhaps you should have an object to retrieve these values instead of a class with static fields. (perhaps with a static cache, although you should synchronize on it before checking/changing it)

    0 讨论(0)
提交回复
热议问题