LruCache为什么需要保存到RetainFragment, why need RetainFragment?

看了android 文档里的bitmapfun例子, 一开始怎么都不明白为什么需要把LruCache放到fragment里面, 后来再仔细回头看了看文档:

Runtime configuration changes, such as a screen orientation change, cause Android to destroy and restart the running activity with the new configuration (For more information about this behavior, see Handling Runtime Changes). You want to avoid having to process all your images again so the user has a smooth and fast experience when a configuration change occurs.

Luckily, you have a nice memory cache of bitmaps that you built in the Use a Memory Cache section. This cache can be passed through to the new activity instance using a Fragment which is preserved by callingsetRetainInstance(true)). After the activity has been recreated, this retained Fragment is reattached and you gain access to the existing cache object, allowing images to be quickly fetched and re-populated into theImageView objects.

 

原来当手机横竖屏变化时, activity会被销毁再创建,为了不丢失LruCache, 所以通过setRetainInstance(true), 保存了cache里的数据, 我就更疑惑了, 为什么不把LruCache设置成全局静态变量, 这样不是程序所有地方都可以共享地用到cache了吗

4 thoughts on “LruCache为什么需要保存到RetainFragment, why need RetainFragment?”

  1. It’s a shame you don’t have a donate button! I’d without a doubt donate to this outstanding blog! I suppose for now i’ll settle for bookmarking and adding your RSS feed to my Google account. I look forward to fresh updates and will share this website with my Facebook group. Talk soon!

  2. Very interesting information!Perfect just what I was looking for! “All the really good ideas I ever had came to me while I was milking a cow.” by Grant Wood.

  3. I’ve been surfing online morte than 4 hourrs today, yet I never found any interesting article like yours.
    It’s pretty worth enough for me. In my view, if all webmasters annd bloggers made good cobtent
    as you did, the net will be much more useful
    than ever before.

Comments are closed.