{"id":542,"date":"2015-04-21T19:46:02","date_gmt":"2015-04-21T11:46:02","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=542"},"modified":"2026-08-15T16:45:48","modified_gmt":"2026-08-15T08:45:48","slug":"%e6%9a%82%e5%81%9c%e4%b8%8e%e6%81%a2%e5%a4%8dactivity","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=542","title":{"rendered":"\u7ba1\u7406Activity\u7684\u751f\u547d\u5468\u671f"},"content":{"rendered":"<h2><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2024\/05\/image.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-3498\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2024\/05\/image.png\" alt=\"\" width=\"2151\" height=\"1105\" srcset=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2024\/05\/image.png 2151w, https:\/\/ai.zhousir.top\/wp-content\/uploads\/2024\/05\/image-768x395.png 768w, https:\/\/ai.zhousir.top\/wp-content\/uploads\/2024\/05\/image-1536x789.png 1536w, https:\/\/ai.zhousir.top\/wp-content\/uploads\/2024\/05\/image-2048x1052.png 2048w\" sizes=\"(max-width: 2151px) 100vw, 2151px\" \/><\/a><\/h2>\n<h2 id=\"activity\">\u542f\u52a8\u4e0e\u9500\u6bc1Activity<\/h2>\n<h3 id=\"activity\">\u6307\u5b9a\u7a0b\u5e8f\u9996\u6b21\u542f\u52a8\u7684Activity<\/h3>\n<ul>\n<li>\u5f53\u7528\u6237\u4ece\u4e3b\u754c\u9762\u70b9\u51fb\u4f60\u7684\u7a0b\u5e8f\u56fe\u6807\u65f6\uff0c\u7cfb\u7edf\u4f1a\u8c03\u7528\u4f60\u7684app\u91cc\u9762\u7684\u88ab\u58f0\u660e\u4e3a&#8221;launcher&#8221; (or &#8220;main&#8221;) activity\u4e2d\u7684onCreate()\u65b9\u6cd5\u3002\u8fd9\u4e2aActivity\u88ab\u7528\u6765\u5f53\u4f5c\u4f60\u7684\u7a0b\u5e8f\u7684\u4e3b\u8981\u8fdb\u5165\u70b9\u3002<\/li>\n<li>\u4f60\u53ef\u4ee5\u5728<a href=\"http:\/\/developer.android.com\/guide\/topics\/manifest\/manifest-intro.html\" target=\"_blank\" rel=\"noopener\">AndroidManifest.xml<\/a>\u4e2d\u5b9a\u4e49\u54ea\u4e2aactivity\u4f5c\u4e3a\u4f60\u7684\u4e3bactivity\u3002<\/li>\n<li>\u8fd9\u4e2amain activity\u5fc5\u987b\u5728manifest\u4f7f\u7528\u5305\u62ec <code>MAIN<\/code> action \u4e0e <code>LAUNCHER<\/code> category \u7684<a href=\"http:\/\/developer.android.com\/guide\/topics\/manifest\/intent-filter-element.html\" target=\"_blank\" rel=\"noopener\"><code>&lt;intent-filter&gt;<\/code><\/a>\u6807\u7b7e\u6765\u58f0\u660e\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n<p><!--more--><\/p>\n<pre><code class=\"lang-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">activity<\/span> <span class=\"hljs-attribute\">android:name<\/span>=<span class=\"hljs-value\">\".MainActivity\"<\/span> <span class=\"hljs-attribute\">android:label<\/span>=<span class=\"hljs-value\">\"@string\/app_name\"<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">intent-filter<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">action<\/span> <span class=\"hljs-attribute\">android:name<\/span>=<span class=\"hljs-value\">\"android.intent.action.MAIN\"<\/span> \/&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-title\">category<\/span> <span class=\"hljs-attribute\">android:name<\/span>=<span class=\"hljs-value\">\"android.intent.category.LAUNCHER\"<\/span> \/&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-title\">intent-filter<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-title\">activity<\/span>&gt;<\/span>\n<\/code><\/pre>\n<blockquote><p><strong>Note<\/strong>:\u5f53\u4f60\u4f7f\u7528Android SDK\u5de5\u5177\u6765\u521b\u5efaAndroid\u5de5\u7a0b\u65f6\uff0c\u5de5\u7a0b\u4e2d\u5c31\u5305\u542b\u4e86\u4e00\u4e2a\u9ed8\u8ba4\u7684\u58f0\u660e\u6709\u8fd9\u4e2afilter\u7684activity\u7c7b\u3002<\/p><\/blockquote>\n<ul>\n<li>\u5982\u679c\u4f60\u7684\u7a0b\u5e8f\u4e2d\u6ca1\u6709\u4e00\u4e2aactivity\u58f0\u660e\u4e86<a href=\"http:\/\/developer.android.com\/reference\/android\/content\/Intent.html#ACTION_MAIN\" target=\"_blank\" rel=\"noopener\">MAIN<\/a> action \u6216\u8005<a href=\"http:\/\/developer.android.com\/reference\/android\/content\/Intent.html#CATEGORY_LAUNCHER\" target=\"_blank\" rel=\"noopener\">LAUNCHER<\/a> category\uff0c\u90a3\u4e48\u5728\u8bbe\u5907\u7684\u4e3b\u754c\u9762\u5217\u8868\u91cc\u9762\u4e0d\u4f1a\u5448\u73b0\u4f60\u7684app\u56fe\u6807\u3002<\/li>\n<\/ul>\n<h3>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b9e\u4f8b<\/h3>\n<ul>\n<li>\u5927\u591a\u6570app\u90fd\u5305\u62ec\u8bb8\u591a\u4e0d\u540c\u7684activity\uff0c\u4f7f\u5f97\u7528\u6237\u53ef\u4ee5\u6267\u884c\u4e0d\u540c\u7684\u52a8\u4f5c\u3002\u4e0d\u8bba\u8fd9\u4e2aactivity\u662f\u5f53\u7528\u6237\u70b9\u51fb\u5e94\u7528\u56fe\u6807\u521b\u5efa\u7684main activtiy\u8fd8\u662f\u4e3a\u4e86\u54cd\u5e94\u7528\u6237\u884c\u4e3a\u800c\u521b\u5efa\u7684\u5176\u4ed6activity\uff0c\u7cfb\u7edf\u90fd\u4f1a\u8c03\u7528\u65b0\u7684activity\u5b9e\u4f8b\u4e2d\u7684onCreate()\u65b9\u6cd5\u3002<\/li>\n<li>\u4f60\u5fc5\u987b\u5b9e\u73b0onCreate()\u65b9\u6cd5\u6765\u6267\u884c\u7a0b\u5e8f\u542f\u52a8\u6240\u9700\u8981\u7684\u57fa\u672c\u903b\u8f91\u3002\u4f8b\u5982\u53ef\u4ee5\u5728onCreate()\u65b9\u6cd5\u4e2d\u5b9a\u4e49UI\u4ee5\u53ca\u5b9e\u4f8b\u5316\u7c7b\u6210\u5458\u53d8\u91cf\u3002<\/li>\n<li>\u4f8b\u5982\uff1a\u4e0b\u9762\u7684onCreate()\u65b9\u6cd5\u6f14\u793a\u4e86\u4e3a\u4e86\u5efa\u7acb\u4e00\u4e2aactivity\u6240\u9700\u8981\u7684\u4e00\u4e9b\u57fa\u7840\u64cd\u4f5c\u3002\u5982\uff0c\u58f0\u660eUI\u5143\u7d20 \uff0c\u5b9a\u4e49\u6210\u5458\u53d8\u91cf\uff0c\u914d\u7f6eUI\u7b49\u3002<em>(onCreate\u91cc\u9762\u5c3d\u91cf\u5c11\u505a\u4e8b\u60c5\uff0c\u907f\u514d\u7a0b\u5e8f\u542f\u52a8\u592a\u4e45\u90fd\u770b\u4e0d\u5230\u754c\u9762)<\/em><\/li>\n<\/ul>\n<pre><code class=\"lang-java\">TextView mTextView; <span class=\"hljs-comment\">\/\/ Member variable for text view in the layout<\/span>\n\n<span class=\"hljs-annotation\">@Override<\/span>\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">onCreate<\/span><span class=\"hljs-params\">(Bundle savedInstanceState)<\/span> <\/span>{\n    <span class=\"hljs-keyword\">super<\/span>.onCreate(savedInstanceState);\n\n    <span class=\"hljs-comment\">\/\/ Set the user interface layout for this Activity<\/span>\n    <span class=\"hljs-comment\">\/\/ The layout file is defined in the project res\/layout\/main_activity.xml<\/span>\n<span class=\"hljs-function\">file\n    <span class=\"hljs-title\">setContentView<\/span><span class=\"hljs-params\">(R.layout.main_activity)<\/span><\/span>;\n\n    <span class=\"hljs-comment\">\/\/ Initialize member TextView so we can manipulate it later<\/span>\n    mTextView = (TextView) findViewById(R.id.text_message);\n\n    <span class=\"hljs-comment\">\/\/ Make sure we're running on Honeycomb or higher to use ActionBar APIs<\/span>\n    <span class=\"hljs-keyword\">if<\/span> (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.HONEYCOMB) {\n        <span class=\"hljs-comment\">\/\/ For the main activity, make sure the app icon in the action bar<\/span>\n        <span class=\"hljs-comment\">\/\/ does not behave as a button<\/span>\n        ActionBar actionBar = getActionBar();\n        actionBar.setHomeButtonEnabled(<span class=\"hljs-keyword\">false<\/span>);\n    }\n}\n<\/code><\/pre>\n<blockquote><p><strong>Caution<\/strong>: Using the <a href=\"http:\/\/developer.android.com\/reference\/android\/os\/Build.VERSION.html#SDK_INT\" target=\"_blank\" rel=\"noopener\">SDK_INT<\/a> to prevent older systems from executing new APIs works in this way on Android 2.0 (API level 5) and higher only. Older versions will encounter a runtime exception.<\/p><\/blockquote>\n<ul>\n<li>\u4e00\u65e6\u7ed3\u675fonCreate \u64cd\u4f5c\uff0c\u7cfb\u7edf\u4f1a\u8fc5\u901f\u8c03\u7528onStart() \u4e0eonResume()\u65b9\u6cd5\u3002\u4f60\u7684activity\u4e0d\u4f1a\u5728Created\u6216\u8005Started\u72b6\u6001\u505c\u7559\u3002\u6280\u672f\u4e0a\u6765\u8bf4, activity\u5728onStart()\u88ab\u8c03\u7528\u540e\u4f1a\u5f00\u59cb\u88ab\u7528\u6237\u53ef\u89c1\uff0c\u4f46\u662f onResume()\u4f1a\u8fc5\u901f\u88ab\u6267\u884c\u4f7f\u5f97activity\u505c\u7559\u5728Resumed\u72b6\u6001\uff0c\u76f4\u5230\u4e00\u4e9b\u56e0\u7d20\u53d1\u751f\u53d8\u5316\u624d\u4f1a\u6539\u53d8\u8fd9\u4e2a\u72b6\u6001\u3002\u4f8b\u5982\u63a5\u53d7\u5230\u4e00\u4e2a\u6765\u7535\uff0c\u7528\u6237\u5207\u6362\u5230\u53e6\u5916\u4e00\u4e2aactivity\uff0c\u6216\u8005\u662f\u8bbe\u5907\u5c4f\u5e55\u5173\u95ed\u3002<\/li>\n<li>\u5728\u540e\u9762\u7684\u8bfe\u7a0b\u4e2d\uff0c\u4f60\u5c06\u770b\u5230\u5176\u4ed6\u65b9\u6cd5\u662f\u5982\u4f55\u4f7f\u7528\u7684\uff0conStart() \u4e0e onResume()\u5728\u7528\u6237\u4ecePaused\u6216 Stopped\u72b6\u6001\u4e2d\u6062\u590d\u7684\u65f6\u5019\u975e\u5e38\u6709\u7528\u3002<\/li>\n<\/ul>\n<blockquote><p><strong>Note:<\/strong> onCreate() \u65b9\u6cd5\u5305\u542b\u4e86\u4e00\u4e2a\u53c2\u6570\u53eb\u505asavedInstanceState\uff0c\u8fd9\u5c06\u4f1a\u5728\u540e\u9762\u7684\u8bfe\u7a0b &#8211; <a href=\"http:\/\/hukai.me\/android-training-course-in-chinese\/activity-lifecycle\/recreating.html\">\u91cd\u65b0\u521b\u5efaactivity<\/a>\u7684\u65f6\u5019\u6d89\u53ca\u5230\u3002<\/p><\/blockquote>\n<ul>\n<li>Figure 2. \u4e0a\u56fe\u663e\u793a\u4e86onCreate(), onStart(), and onResume()\u662f\u5982\u4f55\u6267\u884c\u7684\u3002\u5f53\u8fd9\u4e09\u4e2a\u987a\u5e8f\u6267\u884c\u7684\u56de\u8c03\u51fd\u6570\u5b8c\u6210\u540e\uff0cactivity\u4f1a\u5230\u8fbeResumed\u72b6\u6001\u3002<\/li>\n<\/ul>\n<h3 id=\"activity\">\u9500\u6bc1Activity<\/h3>\n<ul>\n<li>activity\u7684\u7b2c\u4e00\u4e2a\u751f\u547d\u5468\u671f\u56de\u8c03\u51fd\u6570\u662f onCreate(),\u5b83\u6700\u540e\u4e00\u4e2a\u56de\u8c03\u662f<a href=\"http:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onDestroy()\">onDestroy()<\/a>.\u5f53\u7cfb\u7edf\u6536\u5230\u9700\u8981\u5c06\u8be5activity\u5f7b\u5e95\u79fb\u9664\u7684\u4fe1\u53f7\u65f6\uff0c\u7cfb\u7edf\u4f1a\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\u3002<\/li>\n<li>\u5927\u591a\u6570 app\u5e76\u4e0d\u9700\u8981\u5b9e\u73b0\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5c40\u90e8\u7c7b\u7684references\u4f1a\u968f\u7740activity\u7684\u9500\u6bc1\u800c\u9500\u6bc1\uff0c\u5e76\u4e14\u4f60\u7684activity\u5e94\u8be5\u5728onPause()\u4e0eonStop()\u4e2d\u6267\u884c\u6e05\u9664activity\u8d44\u6e90\u7684\u64cd\u4f5c\u3002\u7136\u800c\uff0c\u5982\u679c\u4f60\u7684activity\u542b\u6709\u5728onCreate\u8c03\u7528\u65f6\u521b\u5efa\u7684\u540e\u53f0\u7ebf\u7a0b\uff0c\u6216\u8005\u662f\u5176\u4ed6\u6709\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u7684\u8d44\u6e90\uff0c\u4f60\u5e94\u8be5\u5728OnDestroy()\u65f6\u6740\u6b7b\u540e\u53f0\u7ebf\u7a0b\uff0c\u8fdb\u884c\u8d44\u6e90\u6e05\u7406\u3002<\/li>\n<\/ul>\n<pre><code class=\"lang-java\"><span class=\"hljs-annotation\">@Override<\/span>\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">onDestroy<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n    <span class=\"hljs-keyword\">super<\/span>.onDestroy();  <span class=\"hljs-comment\">\/\/ Always call the superclass<\/span>\n\n    <span class=\"hljs-comment\">\/\/ Stop method tracing that the activity started during onCreate()<\/span>\n    android.os.Debug.stopMethodTracing();\n}\n<\/code><\/pre>\n<blockquote><p><strong>Note:<\/strong> \u7cfb\u7edf\u901a\u5e38\u662f\u5728\u6267\u884c\u4e86onPause()\u4e0eonStop() \u4e4b\u540e\u518d\u8c03\u7528onDestroy() \uff0c\u9664\u975e\u4f60\u7684\u7a0b\u5e8f\u5728onCreate()\u65b9\u6cd5\u91cc\u9762\u5c31\u8c03\u7528\u4e86finish()\u65b9\u6cd5\uff0c\u3002\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f8b\u5982\u4f60\u7684activity\u53ea\u662f\u505a\u4e86\u4e00\u4e2a\u4e34\u65f6\u7684\u903b\u8f91\u8df3\u8f6c\u7684\u529f\u80fd\uff0c\u5b83\u53ea\u662f\u7528\u6765\u51b3\u5b9a\u8df3\u8f6c\u5230\u54ea\u4e00\u4e2aactivity\uff0c\u8fd9\u6837\u7684\u8bdd\uff0c\u4f60\u9700\u8981\u5728onCreate\u91cc\u9762\u53bb\u8c03\u7528finish\u65b9\u6cd5\uff0c\u8fd9\u6837\u7cfb\u7edf\u4f1a\u76f4\u63a5\u5c31\u8c03\u7528onDestory\u65b9\u6cd5\uff0c\u5176\u5b83\u751f\u547d\u5468\u671f\u7684\u65b9\u6cd5\u5219\u4e0d\u4f1a\u88ab\u6267\u884c\u3002<\/p><\/blockquote>\n<h2 id=\"activity\">\u6682\u505c\u4e0e\u6062\u590dActivity<\/h2>\n<ul>\n<li>\u5728\u4f7f\u7528\u901a\u5e38\u7684app\u65f6\uff0c\u524d\u7aef\u7684activity\u6709\u65f6\u5019\u4f1a\u88ab\u5176\u4ed6\u53ef\u89c1\u7684\u7ec4\u4ef6\u800c\u963b\u585e(obstructed)\uff0c\u8fd9\u6837\u4f1a\u5bfc\u81f4\u5f53\u524d\u7684activity\u8fdb\u5165Pause\u72b6\u6001\u3002\u4f8b\u5982\uff0c\u5f53\u6253\u5f00\u4e00\u4e2a\u534a\u900f\u660e\u7684activity\u65f6(\u4f8b\u5982\u4ee5\u5bf9\u8bdd\u6846\u7684\u5f62\u5f0f)\uff0c\u4e4b\u524d\u7684activity\u4f1a\u88ab\u6682\u505c\u3002 \u53ea\u8981\u4e4b\u524d\u7684activity\u4ecd\u7136\u88ab\u90e8\u5206\u53ef\u89c1\uff0c\u8fd9\u4e2aactivity\u5c31\u4f1a\u4e00\u76f4\u5904\u4e8ePaused\u72b6\u6001\u3002<\/li>\n<li>\u7136\u800c\uff0c\u4e00\u65e6\u4e4b\u524d\u7684activity\u88ab\u5b8c\u5168\u963b\u585e\u5e76\u4e0d\u53ef\u89c1\uff0c\u5b83\u5219\u4f1a\u8fdb\u5165Stop\u72b6\u6001(\u5c06\u5728\u4e0b\u4e00\u5c0f\u8282\u8ba8\u8bba)\u3002<\/li>\n<li>\u5f53activity\u8fdb\u5165paused\u72b6\u6001\uff0c\u7cfb\u7edf\u4f1a\u8c03\u7528activity\u4e2d\u7684<a href=\"http:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onPause()\">onPause()<\/a>\u65b9\u6cd5, \u5728\u8fd9\u4e2a\u65b9\u6cd5\u91cc\u9762\u53ef\u4ee5\u6267\u884c\u505c\u6b62\u76ee\u524d\u6b63\u5728\u8fd0\u884c\u7684\u4efb\u52a1\u7684\u64cd\u4f5c\uff0c\u6bd4\u5982\u6682\u505c\u89c6\u9891\u64ad\u653e\u6216\u8005\u662f\u4fdd\u5b58\u90a3\u4e9b\u6709\u53ef\u80fd\u9700\u8981\u957f\u671f\u4fdd\u5b58\u7684\u4fe1\u606f\u3002\u5982\u679c\u7528\u6237\u4ece\u6682\u505c\u72b6\u6001\u56de\u5230\u5f53\u524dactivity\uff0c\u7cfb\u7edf\u5e94\u8be5\u6062\u590d\u90a3\u4e9b\u6570\u636e\u5e76\u6267\u884c<a href=\"http:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onResume()\">onResume()<\/a>\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<p><!--more--><\/p>\n<blockquote><p><strong>Note:<\/strong>\u5f53\u4f60\u7684activity\u8c03\u7528onPause()\uff0c\u90a3\u53ef\u80fd\u610f\u5473\u8005activity\u5c06\u88ab\u6682\u505c\u4e00\u6bb5\u65f6\u95f4\uff0c\u5e76\u4e14\u7528\u6237\u5f88\u53ef\u80fd\u56de\u5230\u4f60\u7684activity\u3002\u7136\u800c\uff0c\u90a3\u4e5f\u662f\u7528\u6237\u8981\u79bb\u5f00\u4f60\u7684activtiy\u7684\u7b2c\u4e00\u4e2a\u4fe1\u53f7\u3002<\/p><\/blockquote>\n<p><strong>Figure 1.<\/strong>\u4e0b\u56fe\u663e\u793a\u4e86\uff0c\u5f53\u4e00\u4e2a\u534a\u900f\u660e\u7684activity\u963b\u585e\u4f60\u7684activity\u65f6\uff0c\u7cfb\u7edf\u4f1a\u8c03\u7528onPause()\u65b9\u6cd5\u5e76\u4e14\u8fd9\u4e2aactivity\u4f1a\u505c\u7559\u5728Paused state (1). \u5982\u679c\u7528\u6237\u5728\u8fd9\u4e2aactivity\u8fd8\u662f\u5728Paused State\u65f6\u56de\u5230\u8fd9\u4e2aactivity\uff0c\u7cfb\u7edf\u5219\u4f1a\u8c03\u7528\u5b83\u7684onResume() (2).<\/p>\n<h3 id=\"activity\">\u6682\u505cActivity<\/h3>\n<ul>\n<li>\u5f53\u7cfb\u7edf\u8c03\u7528activity\u4e2d\u7684onPause()\uff0c\u4ece\u6280\u672f\u4e0a\u8bb2\uff0c\u90a3\u610f\u5473\u7740activity\u4ecd\u7136\u5904\u4e8e\u90e8\u5206\u53ef\u89c1\u7684\u72b6\u6001.\u4f46\u5927\u591a\u6570\u65f6\u5019\uff0c\u90a3\u610f\u5473\u7740\u7528\u6237\u6b63\u5728\u79bb\u5f00\u8fd9\u4e2aactivity\u5e76\u9a6c\u4e0a\u4f1a\u8fdb\u5165Stopped state. \u4f60\u901a\u5e38\u5e94\u8be5\u5728onPause()\u56de\u8c03\u65b9\u6cd5\u91cc\u9762\u505a\u4e0b\u9762\u7684\u4e8b\u60c5:\n<ul>\n<li>\u505c\u6b62\u52a8\u753b\u6216\u8005\u662f\u5176\u4ed6\u6b63\u5728\u8fd0\u884c\u7684\u64cd\u4f5c\uff0c\u90a3\u4e9b\u90fd\u4f1a\u5bfc\u81f4CPU\u7684\u6d6a\u8d39.<\/li>\n<li>\u63d0\u4ea4\u6ca1\u6709\u4fdd\u5b58\u7684\u6539\u53d8\uff0c\u4f46\u662f\u4ec5\u4ec5\u662f\u5728\u7528\u6237\u79bb\u5f00\u65f6\u671f\u5f85\u4fdd\u5b58\u7684\u5185\u5bb9(\u4f8b\u5982\u90ae\u4ef6\u8349\u7a3f).<\/li>\n<li>\u91ca\u653e\u7cfb\u7edf\u8d44\u6e90\uff0c\u4f8b\u5982broadcast receivers, sensors (\u6bd4\u5982GPS), \u6216\u8005\u662f\u5176\u4ed6\u4efb\u4f55\u4f1a\u5f71\u54cd\u5230\u7535\u91cf\u7684\u8d44\u6e90\u3002<\/li>\n<li>\u4f8b\u5982, \u5982\u679c\u4f60\u7684\u7a0b\u5e8f\u4f7f\u7528<a href=\"http:\/\/developer.android.com\/reference\/android\/hardware\/Camera.html\" target=\"_blank\" rel=\"noopener\">Camera<\/a>,onPause()\u4f1a\u662f\u4e00\u4e2a\u6bd4\u8f83\u597d\u7684\u5730\u65b9\u53bb\u505a\u90a3\u4e9b\u91ca\u653e\u8d44\u6e90\u7684\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre><code class=\"lang-java\"><span class=\"hljs-annotation\">@Override<\/span>\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">onPause<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n    <span class=\"hljs-keyword\">super<\/span>.onPause();  <span class=\"hljs-comment\">\/\/ Always call the superclass method first<\/span>\n\n    <span class=\"hljs-comment\">\/\/ Release the Camera because we don't need it when paused<\/span>\n    <span class=\"hljs-comment\">\/\/ and other activities might need to use it.<\/span>\n    <span class=\"hljs-keyword\">if<\/span> (mCamera != <span class=\"hljs-keyword\">null<\/span>) {\n        mCamera.release()\n        mCamera = <span class=\"hljs-keyword\">null<\/span>;\n    }\n}\n<\/code><\/pre>\n<ul>\n<li>\u901a\u5e38\uff0c<strong>\u4e0d\u5e94\u8be5<\/strong>\u4f7f\u7528onPause()\u6765\u4fdd\u5b58\u7528\u6237\u6539\u53d8\u7684\u6570\u636e (\u4f8b\u5982\u586b\u5165\u8868\u683c\u4e2d\u7684\u4e2a\u4eba\u4fe1\u606f) \u5230\u6c38\u4e45\u5b58\u50a8(File\u6216\u8005DB)\u4e0a\u3002\u4ec5\u4ec5\u5f53\u4f60\u786e\u8ba4\u7528\u6237\u671f\u5f85\u90a3\u4e9b\u6539\u53d8\u80fd\u591f\u88ab\u81ea\u52a8\u4fdd\u5b58\u7684\u65f6\u5019(\u4f8b\u5982\u6b63\u5728\u64b0\u5199\u90ae\u4ef6\u8349\u7a3f)\uff0c\u4f60\u53ef\u4ee5\u628a\u90a3\u4e9b\u6570\u636e\u5b58\u5230\u6c38\u4e45\u5b58\u50a8 \u3002\u7136\u800c\uff0c\u4f60\u5e94\u8be5\u907f\u514d\u5728onPause()\u65f6\u6267\u884cCPU-intensive \u7684\u5de5\u4f5c\uff0c\u4f8b\u5982\u5199\u6570\u636e\u5230DB\uff0c\u56e0\u4e3a\u5b83\u4f1a\u5bfc\u81f4\u5207\u6362\u5230\u4e0b\u4e00\u4e2aactivity\u53d8\u5f97\u7f13\u6162(\u4f60\u5e94\u8be5\u628a\u90a3\u4e9bheavy-load\u7684\u5de5\u4f5c\u653e\u5230<a href=\"http:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onStop()\">onStop()<\/a>\u53bb\u505a)\u3002<\/li>\n<li>\u5982\u679c\u4f60\u7684activity\u5b9e\u9645\u4e0a\u662f\u8981\u88abStop\uff0c\u90a3\u4e48\u4f60\u5e94\u8be5\u4e3a\u4e86\u5207\u6362\u7684\u987a\u7545\u800c\u51cf\u5c11\u5728OnPause()\u65b9\u6cd5\u91cc\u9762\u7684\u5de5\u4f5c\u91cf\u3002<\/li>\n<\/ul>\n<blockquote><p><strong>Note:<\/strong>\u5f53\u4f60\u7684<span class=\"glossary-term\" title=\"An activity represents a single screen with a user interface.\" data-glossary-term=\"activity\">activity<\/span>\u5904\u4e8e\u6682\u505c\u72b6\u6001\uff0c<a title=\"An activity represents a single screen with a user interface.\" href=\"http:\/\/developer.android.com\/reference\/android\/app\/%3Cspan%20class=\" data-glossary-term=\"activity\">Activity.html&#8221; target=&#8221;_blank&#8221;&gt;<span class=\"glossary-term\" title=\"An activity represents a single screen with a user interface.\" data-glossary-term=\"activity\">Activity<\/span><\/a>\u5b9e\u4f8b\u662f\u9a7b\u7559\u5728\u5185\u5b58\u4e2d\u7684\uff0c\u5e76\u4e14\u5728<span class=\"glossary-term\" title=\"An activity represents a single screen with a user interface.\" data-glossary-term=\"activity\">activity<\/span> \u6062\u590d\u7684\u65f6\u5019\u91cd\u65b0\u8c03\u7528\u3002\u4f60\u4e0d\u9700\u8981\u5728\u6062\u590d\u5230Resumed\u72b6\u6001\u7684\u4e00\u7cfb\u5217\u56de\u8c03\u65b9\u6cd5\u4e2d\u91cd\u65b0\u521d\u59cb\u5316\u7ec4\u4ef6\u3002<\/p><\/blockquote>\n<h3 id=\"activity\">\u6062\u590dactivity<\/h3>\n<ul>\n<li>\u5f53\u7528\u6237\u4ecePaused\u72b6\u6001\u6062\u590dactivity\u65f6\uff0c\u7cfb\u7edf\u4f1a\u8c03\u7528onResume()\u65b9\u6cd5\u3002<\/li>\n<li>\u8bf7\u6ce8\u610f\uff0c\u7cfb\u7edf\u6bcf\u6b21\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\u65f6\uff0cactivity\u90fd\u5904\u4e8e\u524d\u53f0\uff0c\u5305\u62ec\u7b2c\u4e00\u6b21\u521b\u5efa\u7684\u65f6\u5019\u3002\u6240\u4ee5\uff0c\u5e94\u8be5\u5b9e\u73b0onResume()\u6765\u521d\u59cb\u5316\u90a3\u4e9b\u4f60\u5728onPause\u65b9\u6cd5\u91cc\u9762\u91ca\u653e\u6389\u7684\u7ec4\u4ef6\uff0c\u5e76\u6267\u884c\u90a3\u4e9bactivity\u6bcf\u6b21\u8fdb\u5165Resumed state\u90fd\u9700\u8981\u7684\u521d\u59cb\u5316\u52a8\u4f5c (\u4f8b\u5982\u5f00\u59cb\u52a8\u753b\u4e0e\u521d\u59cb\u5316\u90a3\u4e9b\u53ea\u6709\u5728\u83b7\u53d6\u7528\u6237\u7126\u70b9\u65f6\u624d\u9700\u8981\u7684\u7ec4\u4ef6)<\/li>\n<li>\u4e0b\u9762\u7684onResume()\u7684\u4f8b\u5b50\u662f\u4e0e\u4e0a\u9762\u7684onPause()\u4f8b\u5b50\u76f8\u5bf9\u5e94\u7684\u3002<\/li>\n<\/ul>\n<pre><code class=\"lang-java\"><span class=\"hljs-annotation\">@Override<\/span>\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">onResume<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n    <span class=\"hljs-keyword\">super<\/span>.onResume();  <span class=\"hljs-comment\">\/\/ Always call the superclass method first<\/span>\n\n    <span class=\"hljs-comment\">\/\/ Get the Camera instance as the activity achieves full user focus<\/span>\n    <span class=\"hljs-keyword\">if<\/span> (mCamera == <span class=\"hljs-keyword\">null<\/span>) {\n        initializeCamera(); <span class=\"hljs-comment\">\/\/ Local method to handle camera init<\/span>\n    }\n}\n\n\n<\/code><\/pre>\n<h2 id=\"activity\">\u505c\u6b62\u4e0e\u91cd\u542fActivity<\/h2>\n<ul>\n<li>\u6070\u5f53\u7684\u505c\u6b62\u4e0e\u91cd\u542f\u4f60\u7684activity\u662f\u5f88\u91cd\u8981\u7684\uff0c\u5728activity\u751f\u547d\u5468\u671f\u4e2d\uff0c\u4ed6\u4eec\u80fd\u786e\u4fdd\u7528\u6237\u611f\u77e5\u5230\u7a0b\u5e8f\u7684\u5b58\u5728\u5e76\u4e0d\u4f1a\u4e22\u5931\u4ed6\u4eec\u7684\u8fdb\u5ea6\u3002\u5728\u4e0b\u9762\u4e00\u4e9b\u5173\u952e\u7684\u573a\u666f\u4e2d\u4f1a\u6d89\u53ca\u5230\u505c\u6b62\u4e0e\u91cd\u542f\uff1a\n<ul>\n<li>\u7528\u6237\u6253\u5f00\u6700\u8fd1\u4f7f\u7528app\u7684\u83dc\u5355\u5e76\u5207\u6362\u4f60\u7684app\u5230\u53e6\u5916\u4e00\u4e2aapp\uff0c\u8fd9\u4e2a\u65f6\u5019\u4f60\u7684app\u662f\u88ab\u505c\u6b62\u7684\u3002\u5982\u679c\u7528\u6237\u901a\u8fc7\u624b\u673a\u4e3b\u754c\u9762\u7684\u542f\u52a8\u7a0b\u5e8f\u56fe\u6807\u6216\u8005\u6700\u8fd1\u4f7f\u7528\u7a0b\u5e8f\u7684\u7a97\u53e3\u56de\u5230\u4f60\u7684app\uff0c\u90a3\u4e48\u4f60\u7684activity\u4f1a\u91cd\u542f\u3002<\/li>\n<li>\u7528\u6237\u5728\u4f60\u7684app\u91cc\u9762\u6267\u884c\u542f\u52a8\u4e00\u4e2a\u65b0\u7684activity\u7684\u64cd\u4f5c\uff0c\u5f53\u524dactivity\u4f1a\u5728\u7b2c\u4e8c\u4e2aactivity\u88ab\u521b\u5efa\u540estop\u3002\u5982\u679c\u7528\u6237\u70b9\u51fbback\u6309\u94ae\uff0c\u7b2c\u4e00\u4e2aactivtiy\u4f1a\u88ab\u91cd\u542f\u3002<\/li>\n<li>\u7528\u6237\u5728\u4f7f\u7528\u4f60\u7684app\u65f6\u63a5\u53d7\u5230\u4e00\u4e2a\u6765\u7535\u901a\u8bdd.<\/li>\n<\/ul>\n<\/li>\n<li><a href=\"http:\/\/developer.android.com\/reference\/android\/app\/Activity.html\" target=\"_blank\" rel=\"noopener\">Activity<\/a>\u7c7b\u63d0\u4f9b\u4e86<a href=\"http:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onStop()\">onStop()<\/a>\u4e0e<a href=\"http:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onRestart()\">onRestart()<\/a>\u65b9\u6cd5\u6765\u5141\u8bb8\u5728activity\u505c\u6b62\u4e0e\u91cd\u542f\u65f6\u8fdb\u884c\u8c03\u7528\u3002\u4e0d\u50cf\u6682\u505c\u72b6\u6001\u662f\u90e8\u5206\u963b\u585eUI\uff0c\u505c\u6b62\u72b6\u6001\u662fUI\u4e0d\u518d\u53ef\u89c1\u5e76\u4e14\u7528\u6237\u7684\u7126\u70b9\u8f6c\u79fb\u5230\u53e6\u4e00\u4e2aactivity\u4e2d.<\/li>\n<\/ul>\n<blockquote><p><strong>Note:<\/strong>\u56e0\u4e3a\u7cfb\u7edf\u5728<span class=\"glossary-term\" title=\"An activity represents a single screen with a user interface.\" data-glossary-term=\"activity\">activity<\/span>\u505c\u6b62\u65f6\u4f1a\u5728\u5185\u5b58\u4e2d\u4fdd\u5b58<span class=\"glossary-term\" title=\"An activity represents a single screen with a user interface.\" data-glossary-term=\"activity\">Activity<\/span>\u7684\u5b9e\u4f8b\u3002\u6709\u4e9b\u65f6\u5019\u4f60\u4e0d\u9700\u8981\u5b9e\u73b0onStop(),onRestart()\u751a\u81f3\u662fonStart()\u65b9\u6cd5. \u56e0\u4e3a\u5927\u591a\u6570\u7684<span class=\"glossary-term\" title=\"An activity represents a single screen with a user interface.\" data-glossary-term=\"activity\">activity<\/span>\u76f8\u5bf9\u6bd4\u8f83\u7b80\u5355\uff0c<span class=\"glossary-term\" title=\"An activity represents a single screen with a user interface.\" data-glossary-term=\"activity\">activity<\/span>\u4f1a\u81ea\u5df1\u505c\u6b62\u4e0e\u91cd\u542f\uff0c\u4f60\u53ea\u9700\u8981\u4f7f\u7528onPause()\u6765\u505c\u6b62\u6b63\u5728\u8fd0\u884c\u7684\u52a8\u4f5c\u5e76\u65ad\u5f00\u7cfb\u7edf\u8d44\u6e90\u94fe\u63a5\u3002<\/p><\/blockquote>\n<p><strong>Figure 1.<\/strong>\u4e0a\u56fe\u663e\u793a\uff1a\u5f53\u7528\u6237\u79bb\u5f00\u4f60\u7684activity\uff0c\u7cfb\u7edf\u4f1a\u8c03\u7528onStop()\u6765\u505c\u6b62activity (1). \u8fd9\u4e2a\u65f6\u5019\u5982\u679c\u7528\u6237\u8fd4\u56de\uff0c\u7cfb\u7edf\u4f1a\u8c03\u7528onRestart()(2), \u4e4b\u540e\u4f1a\u8fc5\u901f\u8c03\u7528onStart()(3)\u4e0eonResume()(4). \u8bf7\u6ce8\u610f\uff1a\u65e0\u8bba\u4ec0\u4e48\u539f\u56e0\u5bfc\u81f4activity\u505c\u6b62\uff0c\u7cfb\u7edf\u603b\u662f\u4f1a\u5728onStop()\u4e4b\u524d\u8c03\u7528onPause()\u65b9\u6cd5\u3002<\/p>\n<h3 id=\"activity\">\u505c\u6b62activity<\/h3>\n<ul>\n<li>\u5f53activity\u8c03\u7528onStop()\u65b9\u6cd5, activity\u4e0d\u518d\u53ef\u89c1\uff0c\u5e76\u4e14\u5e94\u8be5\u91ca\u653e\u90a3\u4e9b\u4e0d\u518d\u9700\u8981\u7684\u6240\u6709\u8d44\u6e90\u3002\u4e00\u65e6\u4f60\u7684activity\u505c\u6b62\u4e86\uff0c\u7cfb\u7edf\u4f1a\u5728\u4e0d\u518d\u9700\u8981\u8fd9\u4e2aactivity\u65f6\u6467\u6bc1\u5b83\u7684\u5b9e\u4f8b(<em>\u548c\u6808\u7ed3\u6784\u6709\u5173\uff0c\u901a\u5e38back\u64cd\u4f5c\u4f1a\u5bfc\u81f4\u524d\u4e00\u4e2aactivity\u88ab\u9500\u6bc1<\/em>)\u3002\u5728\u6781\u7aef\u60c5\u51b5\u4e0b\uff0c\u7cfb\u7edf\u4f1a\u76f4\u63a5\u6740\u6b7b\u4f60\u7684app\u8fdb\u7a0b\uff0c\u5e76\u4e14\u4e0d\u6267\u884cactivity\u7684<a href=\"http:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onDestroy()\">onDestroy()<\/a>\u56de\u8c03\u65b9\u6cd5, \u56e0\u6b64\u4f60\u9700\u8981\u4f7f\u7528onStop()\u6765\u91ca\u653e\u8d44\u6e90\uff0c\u4ece\u800c\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\u3002<em>(\u8fd9\u70b9\u9700\u8981\u6ce8\u610f)<\/em><\/li>\n<li>\u5c3d\u7ba1onPause()\u65b9\u6cd5\u662f\u5728onStop()\u4e4b\u524d\u8c03\u7528\uff0c\u4f60\u5e94\u8be5\u4f7f\u7528onStop()\u6765\u6267\u884c\u90a3\u4e9bCPU intensive\u7684shut-down\u64cd\u4f5c\uff0c\u4f8b\u5982writing information to a database.<\/li>\n<li>\u4f8b\u5982\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u5728onStop()\u7684\u65b9\u6cd5\u91cc\u9762\u4fdd\u5b58\u7b14\u8bb0\u8349\u7a3f\u5230persistent storage\u7684\u793a\u4f8b:<\/li>\n<\/ul>\n<pre><code class=\"lang-java\"><span class=\"hljs-annotation\">@Override<\/span>\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">protected<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">onStop<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n    <span class=\"hljs-keyword\">super<\/span>.onStop();  <span class=\"hljs-comment\">\/\/ Always call the superclass method first<\/span>\n\n    <span class=\"hljs-comment\">\/\/ Save the note's current draft, because the activity is stopping<\/span>\n    <span class=\"hljs-comment\">\/\/ and we want to be sure the current note progress isn't lost.<\/span>\n    ContentValues values = <span class=\"hljs-keyword\">new<\/span> ContentValues();\n    values.put(NotePad.Notes.COLUMN_NAME_NOTE, getCurrentNoteText());\n    values.put(NotePad.Notes.COLUMN_NAME_TITLE, getCurrentNoteTitle());\n\n    getContentResolver().update(\n            mUri,    <span class=\"hljs-comment\">\/\/ The URI for the note to update.<\/span>\n            values,  <span class=\"hljs-comment\">\/\/ The map of column names and new values to apply to them.<\/span>\n            <span class=\"hljs-keyword\">null<\/span>,    <span class=\"hljs-comment\">\/\/ No SELECT criteria are used.<\/span>\n            <span class=\"hljs-keyword\">null<\/span>     <span class=\"hljs-comment\">\/\/ No WHERE columns are used.<\/span>\n            );\n}\n<\/code><\/pre>\n<ul>\n<li>\u5f53activity\u5df2\u7ecf\u505c\u6b62\uff0c<a href=\"http:\/\/developer.android.com\/reference\/android\/app\/Activity.html\" target=\"_blank\" rel=\"noopener\">Activity<\/a>\u5bf9\u8c61\u4f1a\u4fdd\u5b58\u5728\u5185\u5b58\u4e2d\uff0c\u5e76\u4e14\u5728activity resume\u7684\u65f6\u5019\u91cd\u65b0\u88ab\u8c03\u7528\u5230\u3002\u4f60\u4e0d\u9700\u8981\u5728\u6062\u590d\u5230Resumed state\u72b6\u6001\u524d\u91cd\u65b0\u521d\u59cb\u5316\u90a3\u4e9b\u88ab\u4fdd\u5b58\u5728\u5185\u5b58\u4e2d\u7684\u7ec4\u4ef6\u3002\u7cfb\u7edf\u540c\u6837\u4fdd\u5b58\u4e86\u6bcf\u4e00\u4e2a\u5728\u5e03\u5c40\u4e2d\u7684\u89c6\u56fe\u7684\u5f53\u524d\u72b6\u6001\uff0c\u5982\u679c\u7528\u6237\u5728EditText\u7ec4\u4ef6\u4e2d\u8f93\u5165\u4e86text\uff0c\u5b83\u4f1a\u88ab\u4fdd\u5b58\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u4fdd\u5b58\u4e0e\u6062\u590d\u5b83\u3002<\/li>\n<\/ul>\n<blockquote><p><strong>Note:<\/strong>\u5373\u4f7f\u7cfb\u7edf\u4f1a\u5728activity stop\u7684\u65f6\u5019\u505c\u6b62\u8fd9\u4e2aactivity\uff0c\u5b83\u4ecd\u7136\u4f1a\u4fdd\u5b58<a href=\"http:\/\/developer.android.com\/reference\/android\/view\/View.html\" target=\"_blank\" rel=\"noopener\">View<\/a>\u5bf9\u8c61\u7684\u72b6\u6001(\u6bd4\u5982<a href=\"http:\/\/developer.android.com\/reference\/android\/widget\/EditText.html\" target=\"_blank\" rel=\"noopener\">EditText<\/a>\u4e2d\u7684\u6587\u5b57) \u5230\u4e00\u4e2a<a href=\"http:\/\/developer.android.com\/reference\/android\/os\/Bundle.html\" target=\"_blank\" rel=\"noopener\">Bundle<\/a>\u4e2d\uff0c\u5e76\u4e14\u5728\u7528\u6237\u8fd4\u56de\u8fd9\u4e2aactivity\u65f6\u6062\u590d\u4ed6\u4eec(\u4e0b\u4e00\u4e2a\u4f1a\u4ecb\u7ecd\u5728activity\u9500\u6bc1\u4e0e\u91cd\u65b0\u5efa\u7acb\u65f6\u5982\u4f55\u4f7f\u7528<a href=\"http:\/\/developer.android.com\/reference\/android\/os\/Bundle.html\" target=\"_blank\" rel=\"noopener\">Bundle<\/a>\u6765\u4fdd\u5b58\u5176\u4ed6\u6570\u636e\u7684\u72b6\u6001).<\/p><\/blockquote>\n<h3 id=\"activity\">\u542f\u52a8\u4e0e\u91cd\u542factivity<\/h3>\n<ul>\n<li>\u5f53\u4f60\u7684activity\u4eceStopped\u72b6\u6001\u56de\u5230\u524d\u53f0\u65f6\uff0c\u5b83\u4f1a\u8c03\u7528onRestart().\u7cfb\u7edf\u518d\u8c03\u7528onStart()\u65b9\u6cd5\uff0conStart()\u65b9\u6cd5\u4f1a\u5728\u6bcf\u6b21\u4f60\u7684activity\u53ef\u89c1\u65f6\u90fd\u4f1a\u88ab\u8c03\u7528\u3002onRestart()\u65b9\u6cd5\u5219\u662f\u53ea\u5728activity\u4ecestopped\u72b6\u6001\u6062\u590d\u65f6\u624d\u4f1a\u88ab\u8c03\u7528\uff0c\u56e0\u6b64\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u6267\u884c\u4e00\u4e9b\u7279\u6b8a\u7684\u6062\u590d(restoration)\u5de5\u4f5c\uff0c\u8bf7\u6ce8\u610f\u4e4b\u524d\u662f\u88abstopped\u800c\u4e0d\u662fdestrory\u3002<\/li>\n<li>\u4f7f\u7528onRestart()\u6765\u6062\u590dactivity\u72b6\u6001\u662f\u4e0d\u592a\u5e38\u89c1\u7684\uff0c\u56e0\u6b64\u5bf9\u4e8e\u8fd9\u4e2a\u65b9\u6cd5\u5982\u4f55\u4f7f\u7528\u6ca1\u6709\u4efb\u4f55\u7684guidelines\u3002\u7136\u800c\uff0c\u56e0\u4e3a\u4f60\u7684onStop()\u65b9\u6cd5\u5e94\u8be5\u505a\u6e05\u9664\u6240\u6709activity\u8d44\u6e90\u7684\u64cd\u4f5c\uff0c\u4f60\u9700\u8981\u5728\u91cd\u65b0\u542f\u52a8activtiy\u65f6\u91cd\u65b0\u5b9e\u4f8b\u5316\u90a3\u4e9b\u88ab\u6e05\u9664\u7684\u8d44\u6e90\uff0c\u540c\u6837, \u4f60\u4e5f\u9700\u8981\u5728activity\u7b2c\u4e00\u6b21\u521b\u5efa\u65f6\u5b9e\u4f8b\u5316\u90a3\u4e9b\u8d44\u6e90\u3002\u4ecb\u4e8e\u4e0a\u9762\u7684\u539f\u56e0\uff0c\u4f60\u5e94\u8be5\u4f7f\u7528onStart()\u4f5c\u4e3aonStop()\u6240\u5bf9\u5e94\u65b9\u6cd5\u3002\u56e0\u4e3a\u7cfb\u7edf\u4f1a\u5728\u521b\u5efaactivity\u4e0e\u4ece\u505c\u6b62\u72b6\u6001\u91cd\u542factivity\u65f6\u90fd\u4f1a\u8c03\u7528onStart().<em>(\u8fd9\u4e2a\u5730\u65b9\u7684\u610f\u601d\u5e94\u8be5\u662f\u8bf4\u4f60\u5728onStop\u91cc\u9762\u505a\u4e86\u54ea\u4e9b\u6e05\u9664\u7684\u64cd\u4f5c\u5c31\u5e94\u8be5\u5728onStart\u91cc\u9762\u91cd\u65b0\u628a\u90a3\u4e9b\u6e05\u9664\u6389\u7684\u8d44\u6e90\u91cd\u65b0\u521b\u5efa\u51fa\u6765)<\/em><\/li>\n<li>\u4f8b\u5982\uff1a\u56e0\u4e3a\u7528\u6237\u5f88\u53ef\u80fd\u5728\u56de\u5230\u8fd9\u4e2aactivity\u4e4b\u524d\u9700\u8981\u8fc7\u4e00\u6bb5\u65f6\u95f4\uff0c\u6240\u4ee5onStart()\u65b9\u6cd5\u662f\u4e00\u4e2a\u6bd4\u8f83\u597d\u7684\u5730\u65b9\u6765\u9a8c\u8bc1\u67d0\u4e9b\u5fc5\u987b\u7684\u7cfb\u7edf\u7279\u6027\u662f\u5426\u53ef\u7528\u3002<\/li>\n<\/ul>\n<pre><code class=\"lang-java\"><span class=\"hljs-annotation\">@Override<\/span>\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">protected<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">onStart<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n    <span class=\"hljs-keyword\">super<\/span>.onStart();  <span class=\"hljs-comment\">\/\/ Always call the superclass method first<\/span>\n\n    <span class=\"hljs-comment\">\/\/ The activity is either being restarted or started for the first time<\/span>\n    <span class=\"hljs-comment\">\/\/ so this is where we should make sure that GPS is enabled<\/span>\n    LocationManager locationManager =\n            (LocationManager) getSystemService(Context.LOCATION_SERVICE);\n    <span class=\"hljs-keyword\">boolean<\/span> gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);\n\n    <span class=\"hljs-keyword\">if<\/span> (!gpsEnabled) {\n        <span class=\"hljs-comment\">\/\/ Create a dialog here that requests the user to enable GPS, and use an intent<\/span>\n        <span class=\"hljs-comment\">\/\/ with the android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS action<\/span>\n        <span class=\"hljs-comment\">\/\/ to take the user to the Settings screen to enable GPS when they click \"OK\"<\/span>\n    }\n}\n\n<span class=\"hljs-annotation\">@Override<\/span>\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">protected<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">onRestart<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n    <span class=\"hljs-keyword\">super<\/span>.onRestart();  <span class=\"hljs-comment\">\/\/ Always call the superclass method first<\/span>\n\n    <span class=\"hljs-comment\">\/\/ Activity being restarted from stopped state<\/span>\n}\n<\/code><\/pre>\n<ul>\n<li>\u5f53\u7cfb\u7edfDestory\u4f60\u7684activity\uff0c\u5b83\u4f1a\u4e3a\u4f60\u7684activity\u8c03\u7528onDestroy()\u65b9\u6cd5\u3002\u56e0\u4e3a\u6211\u4eec\u4f1a\u5728onStop\u65b9\u6cd5\u91cc\u9762\u505a\u91ca\u653e\u8d44\u6e90\u7684\u64cd\u4f5c\uff0c\u90a3\u4e48onDestory\u65b9\u6cd5\u5219\u662f\u4f60\u6700\u540e\u53bb\u6e05\u9664\u90a3\u4e9b\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u7684\u5730\u65b9\u3002\u56e0\u6b64\u4f60\u9700\u8981\u786e\u4fdd\u90a3\u4e9b\u7ebf\u7a0b\u90fd\u88abdestroyed\u5e76\u4e14\u6240\u6709\u7684\u64cd\u4f5c\u90fd\u88ab\u505c\u6b62\u3002<\/li>\n<\/ul>\n<h2 id=\"activity\">\u91cd\u65b0\u521b\u5efaActivity<\/h2>\n<ul>\n<li>\u6709\u51e0\u4e2a\u573a\u666f\u4e2d\uff0cActivity\u662f\u7531\u4e8e\u6b63\u5e38\u7684\u7a0b\u5e8f\u884c\u4e3a\u800c\u88abDestory\u7684\uff0c\u4f8b\u5982\u5f53\u7528\u6237\u70b9\u51fb\u8fd4\u56de\u6309\u94ae\u6216\u8005\u662f\u4f60\u7684Activity\u901a\u8fc7\u8c03\u7528<a href=\"http:\/\/developer.android.com\/reference\/android\/app\/Activity.html#finish()\">finish()<\/a>\u6765\u53d1\u51fa\u505c\u6b62\u4fe1\u53f7\u3002\u7cfb\u7edf\u4e5f\u6709\u53ef\u80fd\u4f1a\u5728\u4f60\u7684Activity\u5904\u4e8estop\u72b6\u6001\u4e14\u957f\u65f6\u95f4\u4e0d\u88ab\u4f7f\u7528\uff0c\u6216\u8005\u662f\u5728\u524d\u53f0activity\u9700\u8981\u66f4\u591a\u7cfb\u7edf\u8d44\u6e90\u7684\u65f6\u5019\u628a\u5173\u95ed\u540e\u53f0\u8fdb\u7a0b\uff0c\u8fd9\u6837\u6765\u83b7\u53d6\u66f4\u591a\u7684\u5185\u5b58\u3002<\/li>\n<li>\u5f53\u4f60\u7684Activity\u662f\u56e0\u4e3a\u7528\u6237\u70b9\u51fbBack\u6309\u94ae\u6216\u8005\u662factivity\u901a\u8fc7\u8c03\u7528finish()\u7ed3\u675f\u81ea\u5df1\u65f6\uff0c\u7cfb\u7edf\u5c31\u4e22\u5931\u4e86\u5bf9Activity\u5b9e\u4f8b\u7684\u5f15\u7528\uff0c\u56e0\u4e3a\u524d\u9762\u7684\u884c\u4e3a\u610f\u5473\u7740\u4e0d\u518d\u9700\u8981\u8fd9\u4e2aactivity\u4e86\u3002\u7136\u800c\uff0c\u5982\u679c\u56e0\u4e3a\u7cfb\u7edf\u8d44\u6e90\u7d27\u5f20\u800c\u5bfc\u81f4Activity\u7684Destory\uff0c \u7cfb\u7edf\u4f1a\u5728\u7528\u6237\u56de\u5230\u8fd9\u4e2aActivity\u65f6\u6709\u8fd9\u4e2aActivity\u5b58\u5728\u8fc7\u7684\u8bb0\u5f55\uff0c\u7cfb\u7edf\u4f1a\u4f7f\u7528\u90a3\u4e9b\u4fdd\u5b58\u7684\u8bb0\u5f55\u6570\u636e\uff08\u63cf\u8ff0\u4e86\u5f53Activity\u88abDestory\u65f6\u7684\u72b6\u6001\uff09\u6765\u91cd\u65b0\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Activity\u5b9e\u4f8b\u3002\u90a3\u4e9b\u88ab\u7cfb\u7edf\u7528\u6765\u6062\u590d\u4e4b\u524d\u72b6\u6001\u800c\u4fdd\u5b58\u7684\u6570\u636e\u88ab\u53eb\u505a &#8220;instance state&#8221; \uff0c\u5b83\u662f\u4e00\u4e9b\u5b58\u653e\u5728<a href=\"http:\/\/developer.android.com\/reference\/android\/os\/Bundle.html\" target=\"_blank\" rel=\"noopener\">Bundle<\/a>\u5bf9\u8c61\u4e2d\u7684key-value pairs\u3002(<em>\u8bf7\u6ce8\u610f\u8fd9\u91cc\u7684\u63cf\u8ff0\uff0c\u8fd9\u5bf9\u7406\u89e3onSaveInstanceState\u6267\u884c\u7684\u65f6\u523b\u5f88\u91cd\u8981<\/em>)<\/li>\n<\/ul>\n<blockquote><p><strong>Caution:<\/strong>\u4f60\u7684<span class=\"glossary-term\" title=\"An activity represents a single screen with a user interface.\" data-glossary-term=\"activity\">Activity<\/span>\u4f1a\u5728\u6bcf\u6b21\u65cb\u8f6c\u5c4f\u5e55\u65f6\u88abdestroyed\u4e0erecreated\u3002\u5f53\u5c4f\u5e55\u6539\u53d8\u65b9\u5411\u65f6\uff0c\u7cfb\u7edf\u4f1aDestory\u4e0eRecreate\u524d\u53f0\u7684<span class=\"glossary-term\" title=\"An activity represents a single screen with a user interface.\" data-glossary-term=\"activity\">activity<\/span>\uff0c\u56e0\u4e3a\u5c4f\u5e55\u914d\u7f6e\u88ab\u6539\u53d8\uff0c\u4f60\u7684<span class=\"glossary-term\" title=\"An activity represents a single screen with a user interface.\" data-glossary-term=\"activity\">Activity<\/span>\u53ef\u80fd\u9700\u8981\u52a0\u8f7d\u4e00\u4e9balternative\u7684\u8d44\u6e90(\u4f8b\u5982layout).<\/p><\/blockquote>\n<ul>\n<li>\u9ed8\u8ba4\u60c5\u51b5\u4e0b, \u7cfb\u7edf\u4f7f\u7528 Bundle \u5b9e\u4f8b\u6765\u4fdd\u5b58\u6bcf\u4e00\u4e2aView(\u89c6\u56fe)\u5bf9\u8c61\u4e2d\u7684\u4fe1\u606f(\u4f8b\u5982\u8f93\u5165EditText \u4e2d\u7684\u6587\u672c\u5185\u5bb9)\u3002\u56e0\u6b64\uff0c\u5982\u679c\u4f60\u7684Activity\u88abdestroyed\u4e0erecreated, \u90a3\u4e48layout\u7684\u72b6\u6001\u4fe1\u606f\u4f1a\u81ea\u52a8\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\u3002\u7136\u800c\uff0c\u4f60\u7684activity\u4e5f\u8bb8\u5b58\u5728\u66f4\u591a\u4f60\u60f3\u8981\u6062\u590d\u7684\u72b6\u6001\u4fe1\u606f\uff0c\u4f8b\u5982\u8bb0\u5f55\u7528\u6237Progress\u7684\u6210\u5458\u53d8\u91cf(member variables)\u3002<\/li>\n<\/ul>\n<blockquote><p><strong>Note<\/strong>:\u4e3a\u4e86\u80fd\u4f7fAndroid\u7cfb\u7edf\u80fd\u591f\u6062\u590d<span class=\"glossary-term\" title=\"An activity represents a single screen with a user interface.\" data-glossary-term=\"activity\">Activity<\/span>\u4e2d\u7684View\u7684\u72b6\u6001\uff0c<strong>\u6bcf\u4e2aView\u90fd\u5fc5\u987b\u6709\u4e00\u4e2a\u552f\u4e00ID<\/strong>\uff0c\u7531<a href=\"http:\/\/developer.android.com\/reference\/android\/view\/View.html#attr_android:id\" target=\"_blank\" rel=\"noopener\">android:id<\/a>\u5b9a\u4e49\u3002<\/p><\/blockquote>\n<ul>\n<li>\u4e3a\u4e86\u8ba9\u4f60\u53ef\u4ee5\u4fdd\u5b58\u989d\u5916\u66f4\u591a\u7684\u6570\u636e\u5230saved instance state\u3002\u5728Activity\u7684\u751f\u547d\u5468\u671f\u91cc\u9762\u5b58\u5728\u4e00\u4e2a\u989d\u5916\u7684\u56de\u8c03\u51fd\u6570\uff0c\u4f60\u5fc5\u987b\u91cd\u5199\u8fd9\u4e2a\u51fd\u6570\u3002\u8fd9\u4e2a\u56de\u8c03\u51fd\u6570\u5e76\u6ca1\u6709\u5728\u524d\u9762\u8bfe\u7a0b\u7684\u56fe\u7247\u793a\u4f8b\u4e2d\u663e\u793a\u3002\u8fd9\u4e2a\u65b9\u6cd5\u662f<a href=\"http:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onSaveInstanceState(android.os.Bundle)\">onSaveInstanceState()<\/a> \uff0c\u5f53\u7528\u6237\u79bb\u5f00\u4f60\u7684Activity\u65f6\uff0c\u7cfb\u7edf\u4f1a\u8c03\u7528\u5b83\u3002\u5f53\u7cfb\u7edf\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u65f6\uff0c\u7cfb\u7edf\u4f1a\u5728\u4f60\u7684Activity\u88ab\u5f02\u5e38Destory\u65f6\u4f20\u9012 Bundle \u5bf9\u8c61\uff0c\u8fd9\u6837\u4f60\u53ef\u4ee5\u589e\u52a0\u989d\u5916\u7684\u4fe1\u606f\u5230Bundle\u4e2d\u5e76\u4fdd\u5b58\u5230\u7cfb\u7edf\u4e2d\u3002\u7136\u540e\u5982\u679c\u7cfb\u7edf\u5728Activity\u88abDestory\u4e4b\u540e\u60f3\u91cd\u65b0\u521b\u5efa\u8fd9\u4e2aActivity\u5b9e\u4f8b\u65f6\uff0c\u4e4b\u524d\u7684\u90a3\u4e2aBundle\u5bf9\u8c61\u4f1a(\u7cfb\u7edf)\u88ab\u4f20\u9012\u5230\u4f60\u7684activity\u7684<a href=\"http:\/\/developer.android.com\/reference\/android\/app\/Activity.html#onRestoreInstanceState(android.os.Bundle)\">onRestoreInstanceState()<\/a>\u65b9\u6cd5\u4e0e onCreate() \u65b9\u6cd5\u4e2d\u3002<\/li>\n<\/ul>\n<p><strong>Figure 2\uff1a\u5f53\u7cfb\u7edf\u5f00\u59cb\u505c\u6b62\u4f60\u7684<span class=\"glossary-term\" title=\"An activity represents a single screen with a user interface.\" data-glossary-term=\"activity\">Activity<\/span>\u65f6\uff0c\u53ea\u6709\u5728<span class=\"glossary-term\" title=\"An activity represents a single screen with a user interface.\" data-glossary-term=\"activity\">Activity<\/span>\u5b9e\u4f8b\u4f1a\u9700\u8981\u91cd\u65b0\u521b\u5efa\u7684\u60c5\u51b5\u4e0b\u624d\u4f1a\u8c03\u7528\u5230<a title=\"An activity represents a single screen with a user interface.\" href=\"http:\/\/developer.android.com\/reference\/android\/app\/%3Cspan%20class=\" data-glossary-term=\"activity\">Activity.html#onSaveInstanceState(android.os.Bundle)&#8221;&gt;onSaveInstanceState()<\/a> (1) \uff0c\u5728\u8fd9\u4e2a\u65b9\u6cd5\u91cc\u9762\u53ef\u4ee5\u6307\u5b9a\u989d\u5916\u7684\u72b6\u6001\u6570\u636e\u5230Bunde\u4e2d\u3002\u5982\u679c\u8fd9\u4e2a<span class=\"glossary-term\" title=\"An activity represents a single screen with a user interface.\" data-glossary-term=\"activity\">Activity<\/span>\u88abdestroyed\u7136\u540e\u8fd9\u4e2a\u5b9e\u4f8b\u53c8\u9700\u8981\u88ab\u91cd\u65b0\u521b\u5efa\u65f6\uff0c\u7cfb\u7edf\u4f1a\u4f20\u9012\u5728 (1) \u4e2d\u7684\u72b6\u6001\u6570\u636e\u5230 onCreate() (2) \u4e0e<a title=\"An activity represents a single screen with a user interface.\" href=\"http:\/\/developer.android.com\/reference\/android\/app\/%3Cspan%20class=\" data-glossary-term=\"activity\">Activity.html#onRestoreInstanceState(android.os.Bundle)&#8221;&gt;onRestoreInstanceState()<\/a>(3).<\/strong><\/p>\n<p><em>(\u901a\u5e38\u6765\u8bf4\uff0c\u8df3\u8f6c\u5230\u5176\u4ed6\u7684activity\u6216\u8005\u662f\u70b9\u51fbHome\u90fd\u4f1a\u5bfc\u81f4\u5f53\u524d\u7684activity\u6267\u884conSaveInstanceState\uff0c\u56e0\u4e3a\u8fd9\u79cd\u60c5\u51b5\u4e0b\u7684activity\u90fd\u662f\u6709\u53ef\u80fd\u4f1a\u88abdestory\u5e76\u4e14\u662f\u9700\u8981\u4fdd\u5b58\u72b6\u6001\u4ee5\u4fbf\u540e\u7eed\u6062\u590d\u4f7f\u7528\u7684\uff0c\u800c\u4ece\u8df3\u8f6c\u7684activity\u70b9\u51fbback\u56de\u5230\u524d\u4e00\u4e2aactivity\uff0c\u90a3\u4e48\u8df3\u8f6c\u524d\u7684activity\u662f\u6267\u884c\u9000\u6808\u7684\u64cd\u4f5c\uff0c\u6240\u4ee5\u8fd9\u79cd\u60c5\u51b5\u4e0b\u662f\u4e0d\u4f1a\u6267\u884conSaveInstanceState\u7684\uff0c\u56e0\u4e3a\u8fd9\u4e2aactivity\u4e0d\u53ef\u80fd\u5b58\u5728\u9700\u8981\u91cd\u5efa\u7684\u64cd\u4f5c)<\/em><\/p>\n<h3 id=\"activity\">\u4fdd\u5b58Activity\u72b6\u6001<\/h3>\n<ul>\n<li>\u5f53\u4f60\u7684activity\u5f00\u59cbStop\uff0c\u7cfb\u7edf\u4f1a\u8c03\u7528 onSaveInstanceState() \uff0c\u4f60\u7684Activity\u53ef\u4ee5\u7528\u952e\u503c\u5bf9\u7684\u96c6\u5408\u6765\u4fdd\u5b58\u72b6\u6001\u4fe1\u606f\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u9ed8\u8ba4\u4fdd\u5b58Activity\u89c6\u56fe\u7684\u72b6\u6001\u4fe1\u606f\uff0c\u4f8b\u5982\u5728 EditText \u7ec4\u4ef6\u4e2d\u7684\u6587\u672c\u6216\u8005\u662f ListView \u7684\u6ed1\u52a8\u4f4d\u7f6e\u3002<\/li>\n<li>\u4e3a\u4e86\u7ed9Activity\u4fdd\u5b58\u989d\u5916\u7684\u72b6\u6001\u4fe1\u606f\uff0c\u4f60\u5fc5\u987b\u5b9e\u73b0onSaveInstanceState() \u5e76\u589e\u52a0key-value pairs\u5230 Bundle \u5bf9\u8c61\u4e2d\uff0c\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n<pre><code class=\"lang-java\"><span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">final<\/span> String STATE_SCORE = <span class=\"hljs-string\">\"playerScore\"<\/span>;\n<span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">final<\/span> String STATE_LEVEL = <span class=\"hljs-string\">\"playerLevel\"<\/span>;\n...\n\n<span class=\"hljs-annotation\">@Override<\/span>\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">onSaveInstanceState<\/span><span class=\"hljs-params\">(Bundle savedInstanceState)<\/span> <\/span>{\n    <span class=\"hljs-comment\">\/\/ Save the user's current game state<\/span>\n    savedInstanceState.putInt(STATE_SCORE, mCurrentScore);\n    savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel);\n\n    <span class=\"hljs-comment\">\/\/ Always call the superclass so it can save the view hierarchy state<\/span>\n    <span class=\"hljs-keyword\">super<\/span>.onSaveInstanceState(savedInstanceState);\n}\n<\/code><\/pre>\n<blockquote><p><strong>Caution<\/strong>: \u603b\u662f\u9700\u8981\u8c03\u7528 onSaveInstanceState() \u65b9\u6cd5\u7684\u7236\u7c7b\u5b9e\u73b0\uff0c\u8fd9\u6837\u9ed8\u8ba4\u7684\u7236\u7c7b\u5b9e\u73b0\u624d\u80fd\u4fdd\u5b58\u89c6\u56fe\u72b6\u6001\u7684\u4fe1\u606f\u3002<\/p><\/blockquote>\n<h3 id=\"activity\">\u6062\u590dActivity\u72b6\u6001<\/h3>\n<ul>\n<li>\u5f53\u4f60\u7684Activity\u4eceDestory\u4e2d\u91cd\u5efa\u3002\u4f60\u53ef\u4ee5\u4ece\u7cfb\u7edf\u4f20\u9012\u7ed9\u4f60\u7684Activity\u7684Bundle\u4e2d\u6062\u590d\u4fdd\u5b58\u7684\u72b6\u6001\u3002 onCreate() \u4e0e onRestoreInstanceState() \u56de\u8c03\u65b9\u6cd5\u90fd\u63a5\u6536\u5230\u4e86\u540c\u6837\u7684Bundle\uff0c\u91cc\u9762\u5305\u542b\u4e86\u540c\u6837\u7684\u5b9e\u4f8b\u72b6\u6001\u4fe1\u606f\u3002<\/li>\n<li>\u56e0\u4e3a onCreate() \u65b9\u6cd5\u4f1a\u5728\u7b2c\u4e00\u6b21\u521b\u5efa\u65b0\u7684Activity\u5b9e\u4f8b\u4e0e\u91cd\u65b0\u521b\u5efa\u4e4b\u524d\u88abDestory\u7684\u5b9e\u4f8b\u65f6\u90fd\u88ab\u8c03\u7528\uff0c\u4f60\u5fc5\u987b\u5728\u5c1d\u8bd5\u8bfb\u53d6 Bundle \u5bf9\u8c61\u524d\u68c0\u6d4b\u5b83\u662f\u5426\u4e3anull\u3002\u5982\u679c\u5b83\u4e3anull\uff0c\u7cfb\u7edf\u5219\u662f\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Activity\u5b9e\u4f8b\uff0c\u800c\u4e0d\u662f\u6062\u590d\u4e4b\u524d\u88abDestory\u7684Activity\u3002<\/li>\n<li>\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a\u6f14\u793a\u5728onCreate\u65b9\u6cd5\u91cc\u9762\u6062\u590d\u4e00\u4e9b\u6570\u636e\uff1a<\/li>\n<\/ul>\n<pre><code class=\"lang-java\"><span class=\"hljs-annotation\">@Override<\/span>\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">protected<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">onCreate<\/span><span class=\"hljs-params\">(Bundle savedInstanceState)<\/span> <\/span>{\n    <span class=\"hljs-keyword\">super<\/span>.onCreate(savedInstanceState); <span class=\"hljs-comment\">\/\/ Always call the superclass first<\/span>\n\n    <span class=\"hljs-comment\">\/\/ Check whether we're recreating a previously destroyed instance<\/span>\n    <span class=\"hljs-keyword\">if<\/span> (savedInstanceState != <span class=\"hljs-keyword\">null<\/span>) {\n        <span class=\"hljs-comment\">\/\/ Restore value of members from saved state<\/span>\n        mCurrentScore = savedInstanceState.getInt(STATE_SCORE);\n        mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);\n    } <span class=\"hljs-keyword\">else<\/span> {\n        <span class=\"hljs-comment\">\/\/ Probably initialize members with default values for a new instance<\/span>\n    }\n    ...\n}\n<\/code><\/pre>\n<ul>\n<li>\u4f60\u4e5f\u53ef\u4ee5\u9009\u62e9\u5b9e\u73b0 onRestoreInstanceState() \uff0c\u800c\u662f\u4e0d\u662f\u5728onCreate\u65b9\u6cd5\u91cc\u9762\u6062\u590d\u6570\u636e\u3002<strong>onRestoreInstanceState()\u65b9\u6cd5\u4f1a\u5728 onStart() \u65b9\u6cd5\u4e4b\u540e\u6267\u884c. \u7cfb\u7edf\u4ec5\u4ec5\u4f1a\u5728\u5b58\u5728\u9700\u8981\u6062\u590d\u7684\u72b6\u6001\u4fe1\u606f\u65f6\u624d\u4f1a\u8c03\u7528 onRestoreInstanceState() \uff0c\u56e0\u6b64\u4f60\u4e0d\u9700\u8981\u68c0\u67e5 Bundle \u662f\u5426\u4e3anull\u3002<\/strong><\/li>\n<\/ul>\n<pre><code class=\"lang-java\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">onRestoreInstanceState<\/span><span class=\"hljs-params\">(Bundle savedInstanceState)<\/span> <\/span>{\n    <span class=\"hljs-comment\">\/\/ Always call the superclass so it can restore the view hierarchy<\/span>\n    <span class=\"hljs-keyword\">super<\/span>.onRestoreInstanceState(savedInstanceState);\n\n    <span class=\"hljs-comment\">\/\/ Restore state members from saved instance<\/span>\n    mCurrentScore = savedInstanceState.getInt(STATE_SCORE);\n    mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);\n}\n<\/code><\/pre>\n<blockquote><p><strong>Caution<\/strong>: \u4e0e\u4e0a\u9762\u4fdd\u5b58\u4e00\u6837\uff0c\u603b\u662f\u9700\u8981\u8c03\u7528onRestoreInstanceState()\u65b9\u6cd5\u7684\u7236\u7c7b\u5b9e\u73b0\uff0c\u8fd9\u6837\u9ed8\u8ba4\u7684\u7236\u7c7b\u5b9e\u73b0\u624d\u80fd\u4fdd\u5b58\u89c6\u56fe\u72b6\u6001\u7684\u4fe1\u606f\u3002\u5982\u679c\u60f3\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u8fd0\u884c\u65f6\u72b6\u6001\u6539\u53d8\u5f15\u8d77\u7684recreate\u4f60\u7684activity\u3002\u8bf7\u53c2\u8003<a href=\"http:\/\/developer.android.com\/guide\/topics\/resources\/runtime-changes.html\" target=\"_blank\" rel=\"noopener\">Handling Runtime Changes<\/a>.<\/p><\/blockquote>\n<pre><code class=\"lang-java\">\u00a0<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u542f\u52a8\u4e0e\u9500\u6bc1Activity \u6307\u5b9a\u7a0b\u5e8f\u9996\u6b21\u542f\u52a8\u7684Activity \u5f53\u7528\u6237\u4ece\u4e3b\u754c\u9762\u70b9\u51fb\u4f60\u7684\u7a0b\u5e8f\u56fe\u6807\u65f6\uff0c\u7cfb\u7edf\u4f1a\u8c03\u7528\u4f60 [&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\/542"}],"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=542"}],"version-history":[{"count":5,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/542\/revisions"}],"predecessor-version":[{"id":3537,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/542\/revisions\/3537"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=542"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=542"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=542"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}