{"id":36419,"date":"2024-11-26T13:07:52","date_gmt":"2024-11-26T05:07:52","guid":{"rendered":"https:\/\/fwq.ai\/blog\/36419\/"},"modified":"2024-11-26T13:07:52","modified_gmt":"2024-11-26T05:07:52","slug":"%e5%9c%a8-android-fragment-%e4%b8%ad%ef%bc%8c%e5%a6%82%e4%bd%95%e6%9c%80%e4%bd%b3%e5%9c%b0%e8%b0%83%e7%94%a8-activity-%e6%96%b9%e6%b3%95%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/36419\/","title":{"rendered":"\u5728 Android Fragment \u4e2d\uff0c\u5982\u4f55\u6700\u4f73\u5730\u8c03\u7528 Activity \u65b9\u6cd5\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173113924840226.jpg\" class=\"aligncenter\" title=\"\u5728 Android Fragment \u4e2d\uff0c\u5982\u4f55\u6700\u4f73\u5730\u8c03\u7528 Activity \u65b9\u6cd5\uff1f\u63d2\u56fe\" alt=\"\u5728 Android Fragment \u4e2d\uff0c\u5982\u4f55\u6700\u4f73\u5730\u8c03\u7528 Activity \u65b9\u6cd5\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>android fragment \u8c03\u7528 activity \u65b9\u6cd5\u7684\u6700\u4f73\u5b9e\u8df5<\/strong><\/p>\n<p>\u5728 android \u5f00\u53d1\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u5728 fragment \u4e2d\u8c03\u7528 activity \u7684\u65b9\u6cd5\u3002\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u6b64\u76ee\u7684\uff0c\u4f46\u5b58\u5728\u4f18\u7f3a\u70b9\u3002<\/p>\n<p><strong>\u63a8\u8350\u65b9\u6cd5\uff1akotlin viewmodel<\/strong><\/p>\n<p>\u5bf9\u4e8e\u4f7f\u7528 kotlin \u7684\u5f00\u53d1\u8005\uff0c\u63a8\u8350\u4f7f\u7528 viewmodel\u3002viewmodel \u662f\u4e00\u79cd\u7528\u4e8e\u5728 fragment \u548c activity \u4e4b\u95f4\u5171\u4eab\u6570\u636e\u7684\u7c7b\u3002\u5b83\u5177\u6709\u4ee5\u4e0b\u4f18\u70b9\uff1a<\/p>\n<ul>\n<li> <strong>\u6570\u636e\u5171\u4eab\uff1a<\/strong>fragment \u548c activity \u53ef\u4ee5\u5171\u4eab viewmodel \u4e2d\u7684\u6570\u636e\uff0c\u4ece\u800c\u907f\u514d\u4e86\u5728\u4e0d\u540c\u751f\u547d\u5468\u671f\u4e4b\u95f4\u4f20\u9012\u6570\u636e\u7684\u9700\u8981\u3002<\/li>\n<li> <strong>\u751f\u547d\u5468\u671f\u611f\u77e5\uff1a<\/strong>viewmodel \u7684\u751f\u547d\u5468\u671f\u4e0e activity \u76f8\u7ed1\u5b9a\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u4f1a\u5728 activity \u88ab\u9500\u6bc1\u65f6\u81ea\u52a8\u6e05\u9664\u3002<\/li>\n<li> <strong>\u53ef\u6d4b\u8bd5\u6027\uff1a<\/strong>viewmodel \u5bb9\u6613\u6d4b\u8bd5\uff0c\u56e0\u4e3a\u5b83\u4e0e\u751f\u547d\u5468\u671f\u65e0\u5173\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<p>\u5728 activity \u4e2d\u521b\u5efa viewmodel\uff1a<\/p>\n<pre>private val viewmodel: myviewmodel by viewmodels()<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728 fragment \u4e2d\u4f7f\u7528 viewmodel\uff1a<\/p>\n<pre>val viewModel: MyViewModel = sharedViewModel()<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u5176\u4ed6\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p><strong>\u901a\u8fc7\u63a5\u53e3\uff1a<\/strong>\u521b\u5efa\u4e00\u4e2a\u63a5\u53e3\uff0c\u8ba9 activity \u5b9e\u73b0\u8be5\u63a5\u53e3\u3002fragment \u901a\u8fc7\u8be5\u63a5\u53e3\u8c03\u7528 activity \u7684\u65b9\u6cd5\u3002<\/p>\n<p><strong>\u901a\u8fc7 setter\/getter \u65b9\u6cd5\uff1a<\/strong>\u5728 activity \u4e2d\u516c\u5f00\u4e00\u4e2a setter \u65b9\u6cd5\uff0c\u8ba9 fragment \u8bbe\u7f6e\u6570\u636e\u3002activity \u4e2d\u8fd8\u63d0\u4f9b\u4e00\u4e2a getter \u65b9\u6cd5\uff0c\u8ba9 fragment \u83b7\u53d6\u6570\u636e\u3002<\/p>\n<p><strong>\u901a\u8fc7 eventbus\uff1a<\/strong>\u4f7f\u7528 eventbus \u7b49\u5e93\u6765\u53d1\u5e03\u4e8b\u4ef6\uff0c\u4ee5\u4fbf fragment \u53ef\u4ee5\u8ba2\u9605\u5e76\u5904\u7406\u8fd9\u4e9b\u4e8b\u4ef6\u3002<\/p>\n<p><strong>\u9009\u62e9\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p>\u6700\u4f73\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7528\u4f8b\u3002\u5982\u679c\u9700\u8981\u5171\u4eab\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u6216\u5904\u7406\u5927\u91cf\u6570\u636e\uff0cviewmodel \u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002\u5982\u679c\u6570\u636e\u4ea4\u4e92\u7b80\u5355\uff0c\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5728 Android Fragment \u4e2d\uff0c\u5982\u4f55\u6700\u4f73\u5730\u8c03\u7528 Activity \u65b9\u6cd5\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>android fragment \u8c03\u7528 activity \u65b9\u6cd5\u7684\u6700\u4f73\u5b9e\u8df5 \u5728 android \u5f00\u53d1\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u5728 fragment \u4e2d\u8c03\u7528 activity \u7684\u65b9\u6cd5\u3002\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u6b64\u76ee\u7684\uff0c\u4f46\u5b58\u5728\u4f18\u7f3a\u70b9\u3002 \u63a8\u8350\u65b9\u6cd5\uff1akotlin viewmodel \u5bf9\u4e8e\u4f7f\u7528 kotlin \u7684\u5f00\u53d1\u8005\uff0c\u63a8\u8350\u4f7f\u7528 viewmodel\u3002viewmodel \u662f\u4e00\u79cd\u7528\u4e8e\u5728 fragment \u548c activity \u4e4b\u95f4\u5171\u4eab\u6570\u636e\u7684\u7c7b\u3002\u5b83\u5177\u6709\u4ee5\u4e0b\u4f18\u70b9\uff1a \u6570\u636e\u5171\u4eab\uff1afragment \u548c activity \u53ef\u4ee5\u5171\u4eab viewmodel \u4e2d\u7684\u6570\u636e\uff0c\u4ece\u800c\u907f\u514d\u4e86\u5728\u4e0d\u540c\u751f\u547d\u5468\u671f\u4e4b\u95f4\u4f20\u9012\u6570\u636e\u7684\u9700\u8981\u3002 \u751f\u547d\u5468\u671f\u611f\u77e5\uff1aviewmodel \u7684\u751f\u547d\u5468\u671f\u4e0e activity \u76f8\u7ed1\u5b9a\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u4f1a\u5728 activity \u88ab\u9500\u6bc1\u65f6\u81ea\u52a8\u6e05\u9664\u3002 \u53ef\u6d4b\u8bd5\u6027\uff1aviewmodel \u5bb9\u6613\u6d4b\u8bd5\uff0c\u56e0\u4e3a\u5b83\u4e0e\u751f\u547d\u5468\u671f\u65e0\u5173\u3002 \u793a\u4f8b\u4ee3\u7801\uff1a \u5728 activity \u4e2d\u521b\u5efa viewmodel\uff1a private val viewmodel: myviewmodel by viewmodels() \u767b\u5f55\u540e\u590d\u5236 \u5728 fragment \u4e2d\u4f7f\u7528 viewmodel\uff1a val viewModel: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-36419","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36419","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=36419"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36419\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=36419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=36419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=36419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}