{"id":535,"date":"2015-04-21T19:30:22","date_gmt":"2015-04-21T11:30:22","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=535"},"modified":"2016-03-03T17:01:49","modified_gmt":"2016-03-03T09:01:49","slug":"%e7%90%86%e8%a7%a3activity%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f%e7%9a%84%e5%9b%9e%e8%b0%83","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=535","title":{"rendered":"\u7406\u89e3Activity\u751f\u547d\u5468\u671f\u7684\u56de\u8c03"},"content":{"rendered":"<ul>\n<li>\u5728\u4e00\u4e2aactivity\u7684\u751f\u547d\u5468\u671f\u4e2d\uff0c\u7cfb\u7edf\u4f1a\u50cf\u91d1\u5b57\u5854\u6a21\u578b\u4e00\u6837\u53bb\u8c03\u7528\u4e00\u7cfb\u5217\u7684\u751f\u547d\u5468\u671f\u56de\u8c03\u65b9\u6cd5\u3002Activity\u751f\u547d\u5468\u671f\u7684\u6bcf\u4e00\u4e2a\u9636\u6bb5\u5c31\u50cf\u91d1\u5b57\u5854\u4e2d\u7684\u53f0\u9636\u3002\u5f53\u7cfb\u7edf\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684activity\u5b9e\u4f8b\uff0c\u6bcf\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u4f1a\u5411\u4e0a\u4e00\u9636\u79fb\u52a8activity\u72b6\u6001\u3002\u5904\u5728\u91d1\u5b57\u5854\u9876\u7aef\u610f\u5473\u7740\u5f53\u524dactivity\u5904\u5728\u524d\u53f0\u5e76\u4e14\u662f\u7528\u6237\u53ef\u4ee5\u4e0e\u5b83\u8fdb\u884c\u4ea4\u4e92\u7684\u72b6\u6001\u3002<\/li>\n<\/ul>\n<p><!--more--><\/p>\n<ul>\n<li>\u5f53\u7528\u6237\u9000\u51fa\u8fd9\u4e2aactivity\u65f6\uff0c\u4e3a\u4e86\u56de\u6536\u8fd9\u4e2aactivity\uff0c\u7cfb\u7edf\u4f1a\u8c03\u7528\u5176\u5b83\u65b9\u6cd5\u6765\u5411\u4e0b\u4e00\u9636\u79fb\u52a8activity\u72b6\u6001\u3002\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0cactivity\u4f1a\u9690\u85cf\u5728\u91d1\u5b57\u5854\u4e0b\u7b49\u5f85(\u4f8b\u5982\u5f53\u7528\u6237\u5207\u6362\u5230\u5176\u4ed6app),\u8fd9\u4e2a\u65f6\u5019activity\u53ef\u4ee5\u91cd\u65b0\u56de\u5230\u9876\u7aef(\u5982\u679c\u7528\u6237\u56de\u5230\u8fd9\u4e2aactivity)\u5e76\u4e14\u6062\u590d\u7528\u6237\u79bb\u5f00\u65f6\u7684\u72b6\u6001\u3002<\/li>\n<\/ul>\n<p>Figure 1. \u4e0b\u9762\u8fd9\u5f20\u56fe\u8bb2\u89e3\u4e86activity\u7684\u751f\u547d\u5468\u671f\uff1a<em>(\u8fd9\u4e2a\u91d1\u5b57\u5854\u6a21\u578b\u8981\u6bd4\u4e4b\u524dDev Guide\u91cc\u9762\u7684\u751f\u547d\u5468\u671f\u56fe\u66f4\u52a0\u5bb9\u6613\u7406\u89e3\uff0c\u66f4\u52a0\u5f62\u8c61)<\/em><\/p>\n<p><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/04\/basic-lifecycle.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-1393\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/04\/basic-lifecycle.png\" alt=\"basic-lifecycle\" width=\"662\" height=\"295\" \/><\/a><\/p>\n<ul>\n<li>\u6839\u636eactivity\u7684\u590d\u6742\u5ea6\uff0c\u4f60\u4e5f\u8bb8\u4e0d\u9700\u8981\u5b9e\u73b0\u6240\u6709\u7684\u751f\u547d\u5468\u671f\u65b9\u6cd5\u3002\u7136\u800c\uff0c\u4f60\u9700\u8981\u77e5\u9053\u6bcf\u4e00\u4e2a\u65b9\u6cd5\u7684\u529f\u80fd\u5e76\u786e\u4fdd\u4f60\u7684app\u80fd\u591f\u50cf\u7528\u6237\u671f\u671b\u7684\u90a3\u6837\u6267\u884c\u3002\u5982\u4f55\u5b9e\u73b0\u4e00\u4e2a\u7b26\u5408\u7528\u6237\u671f\u5f85\u7684app\uff0c\u4f60\u9700\u8981\u6ce8\u610f\u4e0b\u9762\u51e0\u70b9\uff1a\n<ul>\n<li>\u5f53\u4f7f\u7528\u4f60\u7684app\u7684\u65f6\u5019\uff0c\u4e0d\u4f1a\u56e0\u4e3a\u6709\u6765\u7535\u901a\u8bdd\u6216\u8005\u5207\u6362\u5230\u5176\u4ed6app\u800c\u5bfc\u81f4\u7a0b\u5e8fcrash\u3002<\/li>\n<li>\u5f53\u7528\u6237\u6ca1\u6709\u6fc0\u6d3b\u67d0\u4e2a\u7ec4\u4ef6\u7684\u65f6\u5019\u4e0d\u8981\u6d88\u8017\u5b9d\u8d35\u7684\u7cfb\u7edf\u8d44\u6e90\u3002<\/li>\n<li>\u5f53\u79bb\u5f00\u4f60\u7684app\u5e76\u4e14\u4e00\u6bb5\u65f6\u95f4\u540e\u8fd4\u56de\uff0c\u4e0d\u8981\u4e22\u5931\u7528\u6237\u7684\u4f7f\u7528\u8fdb\u5ea6\u3002<\/li>\n<li>\u5f53\u8bbe\u5907\u53d1\u9001\u5c4f\u5e55\u65cb\u8f6c\u7684\u65f6\u5019\uff0c\u4e0d\u4f1acrash\u6216\u8005\u4e22\u5931\u7528\u6237\u7684\u4f7f\u7528\u8fdb\u5ea6\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u5728\u4e0b\u9762\u7684\u8bfe\u7a0b\u4e2d\u4f1a\u4ecb\u7ecd\u4e0a\u56fe\u6240\u793a\u7684\u51e0\u4e2a\u751f\u547d\u72b6\u6001\u3002\u7136\u800c\uff0c\u5176\u4e2d\u53ea\u6709\u4e09\u4e2a\u72b6\u6001\u662f\u9759\u6001\u7684\uff0c\u8fd9\u4e09\u4e2a\u72b6\u6001\u4e0bactivity\u53ef\u4ee5\u5b58\u5728\u4e00\u6bb5\u6bd4\u8f83\u957f\u7684\u65f6\u95f4\u3002<em>(\u5176\u5b83\u51e0\u4e2a\u72b6\u6001\u4f1a\u5f88\u5feb\u5c31\u5207\u6362\u6389\uff0c\u505c\u7559\u7684\u65f6\u95f4\u6bd4\u8f83\u77ed\u6682)<\/em>\n<ul>\n<li><strong>Resumed<\/strong>\uff1a\u5728\u8fd9\u4e2a\u72b6\u6001\uff0cactivity\u5904\u5728\u524d\u53f0\uff0c\u7528\u6237\u53ef\u4ee5\u4e0e\u5b83\u8fdb\u884c\u4ea4\u4e92\u3002(\u901a\u5e38\u4e5f\u88ab\u7406\u89e3\u4e3a&#8221;running&#8221; \u72b6\u6001)<\/li>\n<li><strong>Paused<\/strong>\uff1a\u5728\u8fd9\u4e2a\u72b6\u6001\uff0cactivity\u88ab\u53e6\u5916\u4e00\u4e2aactivity\u6240\u906e\u76d6\uff1a\u53e6\u5916\u7684activity\u6765\u5230\u524d\u53f0\uff0c\u4f46\u662f\u534a\u900f\u660e\u7684\uff0c\u4e0d\u4f1a\u8986\u76d6\u6574\u4e2a\u5c4f\u5e55\u3002\u88ab\u6682\u505c\u7684activity\u4e0d\u4f1a\u518d\u63a5\u53d7\u7528\u6237\u7684\u8f93\u5165\u4e14\u4e0d\u4f1a\u6267\u884c\u4efb\u4f55\u4ee3\u7801\u3002(<em>\u8fd9\u91cc\u7684\u4e0d\u4f1a\u6267\u884c\u4efb\u4f55\u4ee3\u7801\u5e76\u4e0d\u4ee3\u8868\u4e86\u4efb\u4f55\u540e\u53f0\u7ebf\u7a0b\u90fd\u4e0d\u4f1a\u5de5\u4f5c<\/em>)<\/li>\n<li><strong>Stopped<\/strong>\uff1a\u5728\u8fd9\u4e2a\u72b6\u6001, activity\u5b8c\u5168\u88ab\u9690\u85cf\uff0c\u4e0d\u88ab\u7528\u6237\u53ef\u89c1\u3002\u53ef\u4ee5\u8ba4\u4e3a\u662f\u5728\u540e\u53f0\u3002\u5f53stopped, activity\u5b9e\u4f8b\u4e0e\u5b83\u7684\u6240\u6709\u72b6\u6001\u4fe1\u606f\u90fd\u4f1a\u88ab\u4fdd\u7559\uff0c\u4f46\u662factivity\u4e0d\u80fd\u6267\u884c\u4efb\u4f55\u4ee3\u7801\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u5176\u5b83\u72b6\u6001 (<strong>Created<\/strong>\u4e0e<strong>Started<\/strong>)\u90fd\u662f\u77ed\u6682\u7684\uff0c\u7cfb\u7edf\u5feb\u901f\u7684\u6267\u884c\u90a3\u4e9b\u56de\u8c03\u51fd\u6570\u5e76\u901a\u8fc7\u6267\u884c\u4e0b\u4e00\u9636\u6bb5\u7684\u56de\u8c03\u51fd\u6570\u79fb\u52a8\u5230\u4e0b\u4e00\u4e2a\u72b6\u6001\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5728\u7cfb\u7edf\u8c03\u7528<a href=\"http:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onCreate(android.os.Bundle)\">onCreate()<\/a>, \u4e4b\u540e\u4f1a\u8fc5\u901f\u8c03\u7528<a href=\"http:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onStart()\">onStart()<\/a>, \u4e4b\u540e\u518d\u8fc5\u901f\u6267\u884c<a href=\"http:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onResume()\">onResume()<\/a>\u3002\u4e0a\u9762\u5c31\u662f\u57fa\u672c\u7684activity\u751f\u547d\u5468\u671f\u3002<\/li>\n<\/ul>\n<p>Activity\u751f\u547d\u5468\u671f\u4e2d\u7684\u65b9\u6cd5<\/p>\n<p>[java]<br \/>\nvoid onCreate(Bundle savedInstanceState)<br \/>\nvoid onStart()<br \/>\nvoid onRestart()<br \/>\nvoid onResume()<br \/>\nvoid onPause()<br \/>\nvoid onStop()<br \/>\nvoid onDestroy()[\/java]<br \/>\n\u8fd9\u4e03\u4e2a\u65b9\u6cd5\u5b9a\u4e49\u4e86Activity\u7684\u5b8c\u6574\u751f\u547d\u5468\u671f\u3002\u5b9e\u73b0\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u76d1\u89c6\u5176\u4e2d\u7684\u4e09\u4e2a\u5d4c\u5957\u751f\u547d\u5468\u671f\u5faa\u73af\uff1a<\/p>\n<p><strong>Activity<\/strong><strong>\u7684\u5b8c\u6574\u751f\u547d\u5468\u671f<\/strong>\u81ea\u7b2c\u4e00\u6b21\u8c03\u7528onCreate()\u5f00\u59cb\uff0c\u76f4\u81f3\u8c03\u7528onDestroy()\u4e3a\u6b62\u3002Activity\u5728onCreate()\u4e2d\u8bbe\u7f6e\u6240\u6709\u201c\u5168\u5c40\u201d\u72b6\u6001\u4ee5\u5b8c\u6210\u521d\u59cb\u5316\uff0c\u800c\u5728onDestroy()\u4e2d\u91ca\u653e\u6240\u6709\u7cfb\u7edf\u8d44\u6e90\u3002\u4f8b\u5982\uff0c\u5982\u679cActivity\u6709\u4e00\u4e2a\u7ebf\u7a0b\u5728\u540e\u53f0\u8fd0\u884c\u4ece\u7f51\u7edc\u4e0b\u8f7d\u6570\u636e\uff0c\u5b83\u4f1a\u5728onCreate()\u521b\u5efa\u7ebf\u7a0b\uff0c\u800c\u5728 onDestroy()\u9500\u6bc1\u7ebf\u7a0b\u3002<\/p>\n<p><strong>Activity<\/strong><strong>\u7684\u53ef\u89c6\u751f\u547d\u5468\u671f<\/strong>\u81eaonStart()\u8c03\u7528\u5f00\u59cb\u76f4\u5230\u76f8\u5e94\u7684onStop()\u8c03\u7528\u7ed3\u675f\u3002\u5728\u6b64\u671f\u95f4\uff0c\u7528\u6237\u53ef\u4ee5\u5728\u5c4f\u5e55\u4e0a\u770b\u5230Activity\uff0c\u5c3d\u7ba1\u5b83\u4e5f\u8bb8\u5e76\u4e0d\u662f\u4f4d\u4e8e\u524d\u53f0\u6216\u8005\u4e5f\u4e0d\u4e0e\u7528\u6237\u8fdb\u884c\u4ea4\u4e92\u3002\u5728\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u4e4b\u95f4\uff0c\u6211\u4eec\u53ef\u4ee5\u4fdd\u7559\u7528\u6765\u5411\u7528\u6237\u663e\u793a\u8fd9\u4e2aActivity\u6240\u9700\u7684\u8d44\u6e90\u3002\u4f8b\u5982\uff0c\u5f53\u7528\u6237\u4e0d\u518d\u770b\u89c1\u6211\u4eec\u663e\u793a\u7684\u5185\u5bb9\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5728onStart()\u4e2d\u6ce8\u518c\u4e00\u4e2aBroadcastReceiver\u6765\u76d1\u63a7\u4f1a\u5f71\u54cdUI\u7684\u53d8\u5316\uff0c\u800c\u5728onStop()\u4e2d\u6765\u6ce8\u6d88\u3002onStart() \u548c onStop() \u65b9\u6cd5\u53ef\u4ee5\u968f\u7740\u5e94\u7528\u7a0b\u5e8f\u662f\u5426\u4e3a\u7528\u6237\u53ef\u89c1\u800c\u88ab\u591a\u6b21\u8c03\u7528\u3002<\/p>\n<p><strong>Activity<\/strong><strong>\u7684\u524d\u53f0\u751f\u547d\u5468\u671f<\/strong>\u81eaonResume()\u8c03\u7528\u8d77\uff0c\u81f3\u76f8\u5e94\u7684onPause()\u8c03\u7528\u4e3a\u6b62\u3002\u5728\u6b64\u671f\u95f4\uff0cActivity\u4f4d\u4e8e\u524d\u53f0\u6700\u4e0a\u9762\u5e76\u4e0e\u7528\u6237\u8fdb\u884c\u4ea4\u4e92\u3002Activity\u4f1a\u7ecf\u5e38\u5728\u6682\u505c\u548c\u6062\u590d\u4e4b\u95f4\u8fdb\u884c\u72b6\u6001\u8f6c\u6362\u2014\u2014\u4f8b\u5982\u5f53\u8bbe\u5907\u8f6c\u5165\u4f11\u7720\u72b6\u6001\u6216\u8005\u6709\u65b0\u7684Activity\u542f\u52a8\u65f6\uff0c\u5c06\u8c03\u7528onPause() \u65b9\u6cd5\u3002\u5f53Activity\u83b7\u5f97\u7ed3\u679c\u6216\u8005\u63a5\u6536\u5230\u65b0\u7684Intent\u65f6\u4f1a\u8c03\u7528onResume() \u65b9\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u4e00\u4e2aactivity\u7684\u751f\u547d\u5468\u671f\u4e2d\uff0c\u7cfb\u7edf\u4f1a\u50cf\u91d1\u5b57\u5854\u6a21\u578b\u4e00\u6837\u53bb\u8c03\u7528\u4e00\u7cfb\u5217\u7684\u751f\u547d\u5468\u671f\u56de\u8c03\u65b9\u6cd5\u3002Activity\u751f\u547d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/535"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=535"}],"version-history":[{"count":5,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/535\/revisions"}],"predecessor-version":[{"id":1395,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/535\/revisions\/1395"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}