{"id":88,"date":"2014-08-12T20:44:23","date_gmt":"2014-08-12T12:44:23","guid":{"rendered":"http:\/\/www.yarpee.com\/?p=88"},"modified":"2014-08-12T20:44:23","modified_gmt":"2014-08-12T12:44:23","slug":"managing-the-activity-lifecycle","status":"publish","type":"post","link":"http:\/\/www.yarpee.cn\/?p=88","title":{"rendered":"Managing the Activity Lifecycle"},"content":{"rendered":"<p><a href=\"http:\/\/www.yarpee.cn\/wp-content\/uploads\/2014\/08\/Image.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-89\" src=\"http:\/\/www.yarpee.cn\/wp-content\/uploads\/2014\/08\/Image.png\" alt=\"Image\" width=\"662\" height=\"295\" srcset=\"http:\/\/www.yarpee.cn\/wp-content\/uploads\/2014\/08\/Image.png 662w, http:\/\/www.yarpee.cn\/wp-content\/uploads\/2014\/08\/Image-300x133.png 300w\" sizes=\"(max-width: 662px) 100vw, 662px\" \/><\/a><br \/>\nFigure 1. A simplified illustration of the Activity lifecycle, expressed as a step pyramid. This shows how, for every callback used to take the activity a step toward the Resumed state at the top, there&#8217;s a callback method that takes the activity a step down. The activity can also return to the resumed state from the Paused and Stopped state.<br \/>\n\u7528\u6237\u5728Home\u754c\u9762\u9009\u62e9\u4f60\u7684app\u56fe\u6807\u65f6\u7cfb\u7edf\u5c06\u8c03\u7528\u4f60\u6307\u5b9a\u7684&#8221;launcher&#8221;\u6216\u662f&#8221;main&#8221; activity\u7684onCreate\u65b9\u6cd5\u3002\u8be5activity\u5728AndroidManifest.xml\u4e2d\u88ab\u6307\u5b9a\uff0c\u5b83\u5fc5\u987b\u6709\u4e00\u4e2a\u5305\u542bMAIN action\u548cLAUNCHER category\u7684\uff0c\u7f3a\u5c11\u5176\u4e2d\u4efb\u4f55\u4e00\u4e2a\u4f60\u7684app\u56fe\u6807\u5c31\u4e0d\u4f1a\u518dHome\u754c\u9762\u663e\u793a\u3002<\/p>\n<p>\u7cfb\u7edf\u8c03\u7528onDestroy\u4e00\u822c\u662f\u5728\u6309\u987a\u5e8f\u8c03\u7528onPause\u548conStop\u4e4b\u540e\uff0c\u4f46\u6709\u4e00\u4e2a\u4f8b\u5916\uff1a\u5f53\u5728onCreate\u4e2d\u8c03\u7528finish\u65b9\u6cd5\u65f6\u5c06\u4f1a\u89e6\u53d1\u7cfb\u7edf\u76f4\u63a5\u8c03\u7528onDestroy\u3002<\/p>\n<p>* Does not crash if the user receives a phone call or switches to another app while using your app.<br \/>\n* Does not consume valuable system resources when the user is not actively using it.<br \/>\n* Does not lose the user&#8217;s progress if they leave your app and return to it at a later time.<br \/>\n* Does not crash or lose the user&#8217;s progress when the screen rotates between landscape and portrait orientation.<\/p>\n<p>\u5f53activity\u5904\u4e8e\u90e8\u5206\u53ef\u89c1\uff08\u5982activity\u4e0a\u5f39\u51fa\u5bf9\u8bdd\u6846\u65f6\uff09\u65f6\u7cfb\u7edf\u5c06\u8c03\u7528onPause\u65b9\u6cd5\u3002onPause\u4e2d\u9700\u8981\u5904\u7406\u4e00\u4e9b\u5fc5\u8981\u7684\u6570\u636e\u4fdd\u5b58\u5de5\u4f5c\uff0c\u4f46\u4e0d\u8981\u5904\u7406\u5f88\u8017\u65f6\u7684\u5de5\u4f5c\uff08\u5982\u5199\u6570\u636e\u5e93\uff09\u3002\u5f88\u8017\u65f6\u7684\u5de5\u4f5c\u5c06\u5f71\u54cdactivity\u72b6\u6001\u7684\u8f6c\u6362\uff08\u8f6c\u5230onStop\uff09\uff0c\u5e94\u8be5\u653e\u5728onStop\u4e2d\u5904\u7406\u3002<\/p>\n<p>\u6709\u4e9b\u6b63\u5e38\u7684app\u884c\u4e3a\u4f1a\u5bfc\u81f4activity\u88abdestroy\uff0c\u5982\uff1a\u7528\u6237\u6309\u4e86\u8fd4\u56de\u952e\u3001activity\u81ea\u5df1\u8c03\u7528\u4e86finish\u65b9\u6cd5\u3002\u53e6\u4e00\u4e9b\u60c5\u51b5\uff0c\u5982activity\u5904\u4e8estop\u72b6\u6001\u5f88\u957f\u4e00\u6bb5\u65f6\u95f4\u4e86\uff0c\u6216\u8005\u5f53\u524d\u62e5\u6709\u7126\u70b9\u7684activity\u9700\u8981\u66f4\u591a\u7cfb\u7edf\u8d44\u6e90\u5fc5\u987b\u7ec8\u6b62\u540e\u53f0\u8fdb\u7a0b\u65f6\uff0c\u6216\u8005\u65cb\u8f6c\u5c4f\u5e55\u65f6\uff0c\u4e5f\u4f1a\u5bfc\u81f4activity\u88abdestroy\u3002\u4f46\u5bf9\u540e\u4e00\u79cd\u60c5\u51b5\u7cfb\u7edf\u901a\u8fc7Bundle\u4fdd\u5b58\u4e86\u5f53\u65f6activity\u7684\u4fe1\u606f\u3002<\/p>\n<p>\u7cfb\u7edf\u4f1a\u5728\u53d1\u751f\u540e\u4e00\u79cd\u60c5\u51b5\u65f6\u8c03\u7528onSaveInstanceState\u6765\u4fdd\u5b58activity\u5f53\u65f6\u7684\u4fe1\u606f\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u91cd\u5199\u8be5\u65b9\u6cd5\u5b9e\u73b0\u4fdd\u5b58\u81ea\u5b9a\u4e49\u6570\u636e\u3002\u5f53\u7cfb\u7edf\u91cd\u65b0\u521b\u5efa\u8be5activity\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7onCreate\u548conRestoreInstanceState\u6765\u6062\u590d\u3002\u5f53\u6709\u4fdd\u5b58\u7684\u6570\u636e\u65f6\uff0c\u7cfb\u7edf\u4f1a\u5728onStart\u4e4b\u540e\u8c03\u7528onRestoreInstaceState\uff1b\u800c\u5f53\u7528onCreate\u6765\u6062\u590d\u65f6\u9700\u8981\u6ce8\u610f\u5224\u65ad\u53c2\u6570\u662f\u5426\u4e3a\u7a7a\uff0c\u56e0\u4e3a\u65b0\u5efa\u800c\u975e\u91cd\u5efa\u7684activity\u4e5f\u4f1a\u8c03\u7528onCreate\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Figure 1. A simplified illustration of the Activity lif &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/www.yarpee.cn\/?p=88\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201cManaging the Activity Lifecycle\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-88","post","type-post","status-publish","format-standard","hentry","category-android"],"_links":{"self":[{"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=\/wp\/v2\/posts\/88"}],"collection":[{"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=88"}],"version-history":[{"count":1,"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=\/wp\/v2\/posts\/88\/revisions"}],"predecessor-version":[{"id":90,"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=\/wp\/v2\/posts\/88\/revisions\/90"}],"wp:attachment":[{"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=88"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=88"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yarpee.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=88"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}