{"id":724,"date":"2020-07-28T10:03:25","date_gmt":"2020-07-28T02:03:25","guid":{"rendered":"https:\/\/badgameshow.com\/fly\/?p=724"},"modified":"2023-02-03T11:21:50","modified_gmt":"2023-02-03T03:21:50","slug":"jetpack-lifecycle%e5%88%a4%e6%96%b7%e9%a0%81%e9%9d%a2%e5%89%8d%e5%be%8c%e5%8f%b0","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/fly\/jetpack-lifecycle%e5%88%a4%e6%96%b7%e9%a0%81%e9%9d%a2%e5%89%8d%e5%be%8c%e5%8f%b0\/","title":{"rendered":"\ud83d\udca1\u3010Jetpack\u3011Android Lifecycle \u751f\u547d\u9031\u671f \u7bc4\u4f8b\ud83d\udd27"},"content":{"rendered":"<h1>\ud83d\udca1\u3010Jetpack\u3011Android Lifecycle \u751f\u547d\u9031\u671f \u7bc4\u4f8b\ud83d\udd27<\/h1>\n<h4>Android Lifecycle \u662f Android \u5e73\u53f0\u4e0a\u4e00\u500b\u91cd\u8981\u7684\u6280\u8853\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u597d\u5730\u63a7\u5236\u61c9\u7528\u7a0b\u5f0f\u5728\u5404\u7a2e\u4e0d\u540c\u72c0\u614b\u4e4b\u9593\u7684\u8f49\u63db\u3002<\/h4>\n<h4>\u5b83\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7684\u65b9\u6cd5\u4f86\u95dc\u806f\u61c9\u7528\u7a0b\u5f0f\u5143\u4ef6\u7684\u751f\u547d\u9031\u671f\u8b8a\u5316\uff0c\u5f9e\u800c\u5141\u8a31\u958b\u767c\u8005\u4fdd\u6301\u61c9\u7528\u7a0b\u5f0f\u7684\u4e00\u81f4\u6027\u548c\u4e00\u81f4\u6027\u3002<\/h4>\n<h4>Android Lifecycle \u4e5f\u53ef\u4ee5\u70ba\u958b\u767c\u8005\u5275\u9020\u66f4\u597d\u7684\u61c9\u7528\u7a0b\u5f0f\uff0c\u4e26\u4e14\u6e1b\u5c11\u8cc7\u6e90\u6d6a\u8cbb\u548c\u8a18\u61b6\u9ad4\u6d29\u6f0f\u3002<\/h4>\n<h4>\u672c\u6587\u5c07\u8a73\u7d30\u63cf\u8ff0 Android Lifecycle \u7684\u5de5\u4f5c\u539f\u7406\u548c\u7bc4\u4f8b\uff0c\u4ee5\u53ca\u4f7f\u7528\u5b83\u7684\u6700\u4f73\u505a\u6cd5\u3002<\/h4>\n<h2><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/developer.android.com\/static\/images\/topic\/libraries\/architecture\/lifecycle-states.svg\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"https:\/\/developer.android.com\/static\/images\/topic\/libraries\/architecture\/lifecycle-states.svg\" alt=\"\" \/><\/a><\/h2>\n<h4>\u6587\u7ae0\u76ee\u9304<\/h4>\n<ol>\n<li><a href=\"#a\">Lifecycle Application \u5224\u65b7 Activity \u524d\u5f8c\u53f0<\/a><\/li>\n<li><a href=\"#b\">Lifecycle<\/a><\/li>\n<li><a href=\"#c\">Developer Documents Lifecycle<\/a><\/li>\n<\/ol>\n<hr \/>\n<p><a id=\"a\"><\/a><\/p>\n<h4>1.Lifecycle Application \u5224\u65b7 Activity \u524d\u5f8c\u53f0<\/h4>\n<h5>build.gradle<\/h5>\n<pre><code class=\"language-groovy line-numbers\">dependencies {\n    def lifecycle_version = \"2.5.1\"\n    implementation \"androidx.lifecycle:lifecycle-process:$lifecycle_version\"\n}\n<\/code><\/pre>\n<h5>MyApplication.kt<\/h5>\n<pre><code class=\"language-kotlin line-numbers\">class MyApplication : Application(), DefaultLifecycleObserver {\n\n    override fun onCreate() {\n        super&lt;Application&gt;.onCreate()\n\n        ProcessLifecycleOwner.get().lifecycle.addObserver(this)\n    }\n\n    override fun onStart(owner: LifecycleOwner) {\n        super.onStart(owner)\n        Toast.makeText(this, \"\u524d\u53f0\", Toast.LENGTH_SHORT).show()\n    }\n\n    override fun onStop(owner: LifecycleOwner) {\n        super.onStop(owner)\n        Toast.makeText(this, \"\u5f8c\u53f0\", Toast.LENGTH_SHORT).show()\n    }\n}\n<\/code><\/pre>\n<h5>AndroidManifest.xml<\/h5>\n<pre data-language=XML><code class=\"language-markup line-numbers\">&lt;application\n        android:name=\".MyApplication\"&gt;\n&lt;\/application&gt;\n<\/code><\/pre>\n<p><a id=\"b\"><\/a><\/p>\n<h4>2.Lifecycle<\/h4>\n<h5>MyObserver.kt<\/h5>\n<pre><code class=\"language-Kotlin line-numbers\">class MyObserver : DefaultLifecycleObserver {\n\n    override fun onCreate(owner: LifecycleOwner) {\n        super.onCreate(owner)\n        Log.e(MyObserver::class.java.simpleName, \"onCreate\")\n    }\n\n    override fun onStart(owner: LifecycleOwner) {\n        super.onStart(owner)\n        Log.e(MyObserver::class.java.simpleName, \"onStart\")\n    }\n\n    override fun onResume(owner: LifecycleOwner) {\n        super.onResume(owner)\n        Log.e(MyObserver::class.java.simpleName, \"onResume\")\n    }\n\n    override fun onPause(owner: LifecycleOwner) {\n        super.onPause(owner)\n        Log.e(MyObserver::class.java.simpleName, \"onPause\")\n    }\n\n    override fun onStop(owner: LifecycleOwner) {\n        super.onStop(owner)\n        Log.e(MyObserver::class.java.simpleName, \"onStop\")\n    }\n\n    override fun onDestroy(owner: LifecycleOwner) {\n        Log.e(MyObserver::class.java.simpleName, \"onDestroy\")\n        super.onDestroy(owner)\n    }\n}\n<\/code><\/pre>\n<h5>MainActivity.kt<\/h5>\n<pre><code class=\"language-Kotlin line-numbers\">class MainActivity : AppCompatActivity() {\n\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n        setContentView(R.layout.activity_main)\n\n        lifecycle.addObserver(MyObserver())\n    }\n}\n<\/code><\/pre>\n<p><a id=\"c\"><\/a><\/p>\n<h4>3.Developer Documents Lifecycle<\/h4>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/developer.android.com\/topic\/libraries\/architecture\/lifecycle\" title=\"Open in Documents Lifecycle\" target=\"_blank\" rel=\"noopener\">Open in Documents Lifecycle<\/a><\/p>\n\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>\ud83d\udca1\u3010Jetpack\u3011Android Lifecycle \u751f\u547d\u9031\u671f \u7bc4\u4f8b\ud83d\udd27 Android Lifecycle  &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[106],"tags":[107,15,114],"class_list":["post-724","post","type-post","status-publish","format-standard","hentry","category-jetpack","tag-jetpack","tag-kotlin","tag-lifecycle"],"_links":{"self":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/724","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/comments?post=724"}],"version-history":[{"count":8,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/724\/revisions"}],"predecessor-version":[{"id":1841,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/724\/revisions\/1841"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/media?parent=724"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/categories?post=724"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/tags?post=724"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}