{"id":1380,"date":"2012-12-23T11:15:40","date_gmt":"2012-12-23T03:15:40","guid":{"rendered":"http:\/\/learn-house.idv.tw\/?p=1380"},"modified":"2012-12-23T14:44:04","modified_gmt":"2012-12-23T06:44:04","slug":"%e4%bd%95%e8%ac%82android-context","status":"publish","type":"post","link":"https:\/\/learn-house.idv.tw\/?p=1380","title":{"rendered":"[Android]\u4f55\u8b02Android Context?"},"content":{"rendered":"<p>\u4ee5\u4e0b\u662f\u7db2\u8def\u4e0a\u627e\u5230\u7684\u5c0dandroid context\u7684\u4e00\u4e9b\u8b1b\u89e3<\/p>\n<p>\u5728android\u4e2dcontext\u53ef\u4ee5\u4f5c\u5f88\u591a\u64cd\u4f5c\uff0c\u4f46\u662f\u6700\u4e3b\u8981\u7684\u529f\u80fd\u662f\u52a0\u8f09\u548c\u8a2a\u554f\u8cc7\u6e90\u3002\u5728android\u4e2d\u6709\u5169\u7a2econtext\uff0c\u4e00\u7a2e\u662f application context\uff0c\u4e00\u7a2e\u662factivity context\uff0c\u901a\u5e38\u6211\u5011\u5728\u5404\u7a2e\u985e\u548c\u65b9\u6cd5\u9593\u50b3\u905e\u7684\u662factivity context\u3002<br \/>\n\u6bd4\u5982\u4e00\u500bactivity\u7684onCreate\uff1a<!--more--><br \/>\nprotected void onCreate(Bundle state) {<br \/>\nsuper.onCreate(state);<\/p>\n<p>TextView label = new TextView(this); \/\/\u50b3\u905econtext\u7d66view control<br \/>\nlabel.setText(\"Leaks are bad\");<\/p>\n<p>setContentView(label);<br \/>\n}<br \/>\n\u628aactivity context\u50b3\u905e\u7d66view\uff0c\u610f\u5473\u8457view\u64c1\u6709\u4e00\u500b\u6307\u5411activity\u7684\u5f15\u7528\uff0c\u9032\u800c\u5f15\u7528activity\u4f54\u6709\u7684\u8cc7\u6e90\uff1aview hierachy, resource\u7b49\u3002<br \/>\n\u9019\u6a23\u5982\u679ccontext\u767c\u751f\u5167\u5b58\u6d29\u9732\u7684\u8a71\uff0c\u5c31\u6703\u6d29\u9732\u5f88\u591a\u5167\u5b58\u3002<br \/>\n\u9019\u88e1\u6d29\u9732\u7684\u610f\u601d\u662fgc\u6c92\u6709\u8fa6\u6cd5\u56de\u6536activity\u7684\u5167\u5b58\u3002<\/p>\n<p>Leaking an entire activity\u662f\u5f88\u5bb9\u6613\u7684\u4e00\u4ef6\u4e8b\u3002<\/p>\n<p>\u7576\u5c4f\u5e55\u65cb\u8f49\u7684\u6642\u5019\uff0c\u7cfb\u7d71\u6703\u92b7\u6bc0\u7576\u524d\u7684activity\uff0c\u4fdd\u5b58\u72c0\u614b\u4fe1\u606f\uff0c\u518d\u5275\u5efa\u4e00\u500b\u65b0\u7684\u3002<\/p>\n<p>\u6bd4\u5982\u6211\u5011\u5beb\u4e86\u4e00\u500b\u61c9\u7528\u7a0b\u5e8f\uff0c\u5b83\u9700\u8981\u52a0\u8f09\u4e00\u500b\u5f88\u5927\u7684\u5716\u7247\uff0c\u6211\u5011\u4e0d\u5e0c\u671b\u6bcf\u6b21\u65cb\u8f49\u5c4f \u5e55\u7684\u6642\u5019\u90fd\u92b7\u6bc0\u9019\u500b\u5716\u7247\uff0c\u91cd\u65b0\u52a0\u8f09\u3002\u5be6\u73fe\u9019\u500b\u8981\u6c42\u7684\u7c21\u55ae\u60f3\u6cd5\u5c31\u662f\u5b9a\u7fa9\u4e00\u500b\u975c\u614b\u7684Drawable\uff0c\u9019\u6a23Activity \u985e\u5275\u5efa\u92b7\u6bc0\u5b83\u59cb\u7d42\u4fdd\u5b58\u5728\u5167\u5b58\u4e2d\u3002<br \/>\n\u5be6\u73fe\u985e\u4f3c\uff1a<br \/>\npublic class myactivity extends Activity {<br \/>\nprivate static Drawable sBackground;<br \/>\nprotected void onCreate(Bundle state) {<br \/>\nsuper.onCreate(state);<\/p>\n<p>TextView label = new TextView(this);<br \/>\nlabel.setText(\"Leaks are bad\");<\/p>\n<p>if (sBackground == null) {<br \/>\nsBackground = getDrawable(R.drawable.large_bitmap);<br \/>\n}<br \/>\nlabel.setBackgroundDrawable(sBackground);\/\/drawable attached to a view<\/p>\n<p>setContentView(label);<br \/>\n}<br \/>\n}<br \/>\n\u9019\u6bb5\u7a0b\u5e8f\u770b\u8d77\u4f86\u5f88\u7c21\u55ae\uff0c\u4f46\u662f\u537b\u554f\u984c\u5f88\u5927\u3002\u7576\u5c4f\u5e55\u65cb\u8f49\u7684\u6642\u5019\u6703\u6709leak\uff08\u5373gc\u6c92\u6cd5\u92b7\u6bc0activity\uff09\u3002<br \/>\n\u6211\u5011\u525b\u624d\u8aaa\u904e\uff0c\u5c4f\u5e55\u65cb\u8f49\u7684\u6642\u5019\u7cfb\u7d71\u6703\u92b7\u6bc0\u7576\u524d\u7684activity\u3002\u4f46\u662f\u7576drawable\u548cview\u95dc\u806f\u5f8c\uff0cdrawable\u4fdd\u5b58\u4e86view\u7684 reference\uff0c\u5373sBackground\u4fdd\u5b58\u4e86label\u7684\u5f15\u7528\uff0c\u800clabel\u4fdd\u5b58\u4e86activity\u7684\u5f15\u7528\u3002\u65e2\u7136drawable\u4e0d\u80fd\u92b7\u6bc0\uff0c\u5b83\u6240 \u5f15\u7528\u548c\u9593\u63a5\u5f15\u7528\u7684\u90fd\u4e0d\u80fd\u92b7\u6bc0\uff0c\u9019\u6a23\u7cfb\u7d71\u5c31\u6c92\u6709\u8fa6\u6cd5\u92b7\u6bc0\u7576\u524d\u7684activity\uff0c\u65bc\u662f\u9020\u6210\u4e86\u5167\u5b58\u6d29\u9732\u3002gc\u5c0d\u9019\u7a2e\u985e\u578b\u7684\u5167\u5b58\u6d29\u9732\u662f\u7121\u80fd\u70ba\u529b\u7684\u3002<\/p>\n<p>\u907f\u514d\u9019\u7a2e\u5167\u5b58\u6d29\u9732\u7684\u65b9\u6cd5\u662f\u907f\u514dactivity\u4e2d\u7684\u4efb\u4f55\u5c0d\u8c61\u7684\u751f\u547d\u9031\u671f\u9577\u904eactivity\uff0c\u907f\u514d\u7531\u65bc\u5c0d\u8c61\u5c0d activity\u7684\u5f15\u7528\u5c0e\u81f4activity\u4e0d\u80fd\u6b63\u5e38\u88ab\u92b7\u6bc0\u3002\u6211\u5011\u53ef\u4ee5\u4f7f\u7528application context\u3002application context\u4f34\u96a8application\u7684\u4e00\u751f\uff0c\u8207activity\u7684\u751f\u547d\u9031\u671f\u7121\u95dc\u3002application context\u53ef\u4ee5\u901a\u904eContext.getApplicationContext\u6216\u8005Activity.getApplication\u65b9\u6cd5\u7372\u53d6\u3002<\/p>\n<p>\u907f\u514dcontext\u76f8\u95dc\u7684\u5167\u5b58\u6d29\u9732\uff0c\u8a18\u4f4f\u4ee5\u4e0b\u5e7e\u9ede\uff1a<br \/>\n1. \u4e0d\u8981\u8b93\u751f\u547d\u9031\u671f\u9577\u7684\u5c0d\u8c61\u5f15\u7528activity context\uff0c\u5373\u4fdd\u8b49\u5f15\u7528activity\u7684\u5c0d\u8c61\u8981\u8207activity\u672c\u8eab\u751f\u547d\u9031\u671f\u662f\u4e00\u6a23\u7684<br \/>\n2. \u5c0d\u65bc\u751f\u547d\u9031\u671f\u9577\u7684\u5c0d\u8c61\uff0c\u53ef\u4ee5\u4f7f\u7528application context<\/p>\n<p>\u8cc7\u6599\u4f86\u6e90\uff1ahttp:\/\/bbs.csdn.net\/topics\/360043738<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee5\u4e0b\u662f\u7db2\u8def\u4e0a\u627e\u5230\u7684\u5c0dandroid context\u7684\u4e00\u4e9b\u8b1b\u89e3 \u5728android\u4e2dcontext\u53ef\u4ee5\u4f5c\u5f88\u591a\u64cd\u4f5c\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,5],"tags":[],"class_list":["post-1380","post","type-post","status-publish","format-standard","hentry","category-12","category-5"],"_links":{"self":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/1380"}],"collection":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1380"}],"version-history":[{"count":0,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/1380\/revisions"}],"wp:attachment":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1380"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1380"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1380"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}