{"id":325,"date":"2013-02-17T22:15:54","date_gmt":"2013-02-17T14:15:54","guid":{"rendered":"http:\/\/www.genepeng.com\/?p=325"},"modified":"2013-02-17T22:15:54","modified_gmt":"2013-02-17T14:15:54","slug":"lrucache%e4%b8%ba%e4%bb%80%e4%b9%88%e9%9c%80%e8%a6%81%e4%bf%9d%e5%ad%98%e5%88%b0retainfragment%ef%bc%8c-why-need-retainfragment","status":"publish","type":"post","link":"https:\/\/www.genepeng.com\/index.php\/325","title":{"rendered":"LruCache\u4e3a\u4ec0\u4e48\u9700\u8981\u4fdd\u5b58\u5230RetainFragment\uff0c why need RetainFragment?"},"content":{"rendered":"<p>\u770b\u4e86android \u6587\u6863\u91cc\u7684bitmapfun\u4f8b\u5b50\uff0c \u4e00\u5f00\u59cb\u600e\u4e48\u90fd\u4e0d\u660e\u767d\u4e3a\u4ec0\u4e48\u9700\u8981\u628a<span style=\"font-family: Consolas, Monaco, monospace; font-size: 12px; line-height: 18px;\">LruCache\u653e\u5230fragment\u91cc\u9762, \u540e\u6765\u518d\u4ed4\u7ec6\u56de\u5934\u770b\u4e86\u770b\u6587\u6863\uff1a<\/span><\/p>\n<p>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\u00a0<a href=\"http:\/\/developer.android.com\/guide\/topics\/resources\/runtime-changes.html\">Handling Runtime Changes<\/a>). 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.<\/p>\n<p>Luckily, you have a nice memory cache of bitmaps that you built in the\u00a0<a href=\"http:\/\/developer.android.com\/training\/displaying-bitmaps\/cache-bitmap.html#memory-cache\">Use a Memory Cache<\/a>\u00a0section. This cache can be passed through to the new activity instance using a\u00a0<code><a href=\"http:\/\/developer.android.com\/reference\/android\/app\/Fragment.html\">Fragment<\/a><\/code>\u00a0which is preserved by calling<code><a href=\"http:\/\/developer.android.com\/reference\/android\/app\/Fragment.html#setRetainInstance(boolean)\">setRetainInstance(true)<\/a><\/code>). After the activity has been recreated, this retained\u00a0<code><a href=\"http:\/\/developer.android.com\/reference\/android\/app\/Fragment.html\">Fragment<\/a><\/code>\u00a0is reattached and you gain access to the existing cache object, allowing images to be quickly fetched and re-populated into the<code><a href=\"http:\/\/developer.android.com\/reference\/android\/widget\/ImageView.html\">ImageView<\/a><\/code>\u00a0objects.<\/p>\n<p>&nbsp;<\/p>\n<p>\u539f\u6765\u5f53\u624b\u673a\u6a2a\u7ad6\u5c4f\u53d8\u5316\u65f6\uff0c activity\u4f1a\u88ab\u9500\u6bc1\u518d\u521b\u5efa\uff0c\u4e3a\u4e86\u4e0d\u4e22\u5931LruCache\uff0c \u6240\u4ee5\u901a\u8fc7<a href=\"http:\/\/developer.android.com\/reference\/android\/app\/Fragment.html#setRetainInstance(boolean)\">setRetainInstance(true)<\/a>\uff0c \u4fdd\u5b58\u4e86cache\u91cc\u7684\u6570\u636e\uff0c \u6211\u5c31\u66f4\u7591\u60d1\u4e86\uff0c \u4e3a\u4ec0\u4e48\u4e0d\u628aLruCache\u8bbe\u7f6e\u6210\u5168\u5c40\u9759\u6001\u53d8\u91cf\uff0c \u8fd9\u6837\u4e0d\u662f\u7a0b\u5e8f\u6240\u6709\u5730\u65b9\u90fd\u53ef\u4ee5\u5171\u4eab\u5730\u7528\u5230cache\u4e86\u5417<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u770b\u4e86android \u6587\u6863\u91cc\u7684bitmapfun\u4f8b\u5b50\uff0c \u4e00\u5f00\u59cb\u600e\u4e48\u90fd\u4e0d\u660e\u767d\u4e3a\u4ec0\u4e48\u9700\u8981\u628aLruCache\u653e\u5230fragment\u91cc\u9762, \u540e\u6765\u518d\u4ed4\u7ec6\u56de\u5934\u770b\u4e86\u770b\u6587\u6863\uff1a 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\u00a0Handling Runtime Changes). You want to avoid having to process all your images again so the user has a smooth and fast experience &#8230; <a title=\"LruCache\u4e3a\u4ec0\u4e48\u9700\u8981\u4fdd\u5b58\u5230RetainFragment\uff0c why need RetainFragment?\" class=\"read-more\" href=\"https:\/\/www.genepeng.com\/index.php\/325\" aria-label=\"More on LruCache\u4e3a\u4ec0\u4e48\u9700\u8981\u4fdd\u5b58\u5230RetainFragment\uff0c why need RetainFragment?\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[61],"tags":[],"_links":{"self":[{"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/posts\/325"}],"collection":[{"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/comments?post=325"}],"version-history":[{"count":2,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/posts\/325\/revisions"}],"predecessor-version":[{"id":328,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/posts\/325\/revisions\/328"}],"wp:attachment":[{"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/media?parent=325"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/categories?post=325"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/tags?post=325"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}