{"id":772,"date":"2025-08-21T13:48:17","date_gmt":"2025-08-21T13:48:17","guid":{"rendered":"https:\/\/boochlin.com\/?p=772"},"modified":"2025-11-12T12:17:33","modified_gmt":"2025-11-12T12:17:33","slug":"communicate-with-fragments","status":"publish","type":"post","link":"https:\/\/boochlin.com\/?p=772","title":{"rendered":"Communicate with fragments"},"content":{"rendered":"<p><a href=\"https:\/\/developer.android.com\/guide\/fragments\/communicate\">\u53c3\u8003\u539f\u6587<\/a><\/p>\n<p>\u8acb\u5fd8\u6389 getActivity() \u6216 findFragmentByTag()\u3002<\/p>\n<p>\u73fe\u4ee3\u5316\u7684 Fragment \u901a\u8a0a\uff0c\u6838\u5fc3\u662f\u300c\u4e2d\u4ecb\u8005\u6a21\u5f0f\u300d\u3002\u4f60\u6709\u5169\u7a2e\u6b66\u5668\uff1aViewModel \u548c Fragment Result API\u3002<\/p>\n<h4 class=\"wp-block-heading\"><strong>\u4e00\uff1aViewModel &#8211; <\/strong>\u6301\u7e8c\u6027\u7684<\/h4>\n<p>\u7576\u591a\u500b Fragment \u9700\u8981\u5171\u4eab\u300c\u6301\u7e8c\u6027\u7684\u72c0\u614b\u300d\u6642\uff0c\u5c31\u7528 ViewModel\u3002\u4f46\u95dc\u9375\u662f\uff0c\u9019\u500b\u300c\u4f48\u544a\u6b04\u300d\u8981\u653e\u5728\u54ea\uff1f\u9019\u53d6\u6c7a\u65bc\u4f60\u9078\u64c7\u7684\u4f5c\u7528\u57df\uff08Scope\uff09\u3002<\/p>\n<p><strong>1. by viewModels()\uff1a\u79c1\u4eba\u7b46\u8a18\u672c<\/strong> \u4f5c\u7528\u57df\u7d81\u5b9a Fragment \u81ea\u5df1\uff0c\u72c0\u614b\u4e0d\u5916\u6d41\u3002<\/p>\n<p>Kotlin<\/p>\n<pre class=\"wp-block-syntaxhighlighter-code\">\/\/ ViewModel \u53ea\u5c6c\u65bc\u9019\u500b Fragment\nprivate val viewModel: MyPrivateViewModel by viewModels()\n<\/pre>\n<p><strong>2. by activityViewModels()\uff1a\u5927\u5ef3\u516c\u7528\u4f48\u544a\u6b04<\/strong> \u4f5c\u7528\u57df\u7d81\u5b9a\u5bbf\u4e3b Activity\uff0c\u9069\u5408\u8de8 Fragment \u5171\u4eab\u3002<\/p>\n<p>Kotlin<\/p>\n<pre class=\"wp-block-syntaxhighlighter-code\">\/\/ \u6240\u6709\u4f9d\u9644\u65bc\u540c\u4e00\u500b Activity \u7684 Fragment \u90fd\u80fd\u62ff\u5230\u5b83\nprivate val sharedViewModel: SharedViewModel by activityViewModels()\n<\/pre>\n<p><strong>3. by navGraphViewModels(graphId)\uff1a\u5c08\u6848\u5718\u968a\u7684\u767d\u677f<\/strong> \u4f5c\u7528\u57df\u7d81\u5b9a\u4e00\u500b Navigation Graph\uff0c\u9069\u5408\u7279\u5b9a\u696d\u52d9\u6d41\u7a0b\uff08\u5982\u8a3b\u518a\u3001\u7d50\u5e33\uff09\u4e2d\u7684\u6578\u64da\u5171\u4eab\u3002<\/p>\n<p>Kotlin<\/p>\n<pre class=\"wp-block-syntaxhighlighter-code\">\/\/ \u53ea\u6709\u5728 R.id.signup_flow \u9019\u500b\u5c0e\u822a\u5716\u4e2d\u7684 Fragment \u80fd\u62ff\u5230\u5b83\nprivate val signupViewModel: SignupViewModel by navGraphViewModels(R.id.signup_flow)\n<\/pre>\n<h4 class=\"wp-block-heading\"><strong>\u4e8c\uff1aFragment Result API &#8211; \u4e00\u6b21\u6027<\/strong><\/h4>\n<p>\u7576\u4f60\u9700\u8981\u300c\u4e00\u6b21\u6027\u7684\u7d50\u679c\u56de\u50b3\u300d\u6642\uff0c\u4f8b\u5982 A \u53eb B \u53bb\u9078\u500b\u65e5\u671f\u518d\u544a\u8a34 A\uff0c\u5c31\u7528\u9019\u500b\u3002<\/p>\n<p><strong>1. \u63a5\u6536\u65b9 (RequestFragment)\uff1a\u767b\u8a18\u6536\u4fe1<\/strong> \u7528 setFragmentResultListener \u76e3\u807d\u4e00\u500b\u7279\u5b9a\u9470\u5319 requestKey\u3002<\/p>\n<p>Kotlin<\/p>\n<pre class=\"wp-block-syntaxhighlighter-code\">override fun onCreate(savedInstanceState: Bundle?) {\n\u00a0 \u00a0 super.onCreate(savedInstanceState)\n\u00a0 \u00a0 setFragmentResultListener(\"requestKey\") { key, bundle ->\n\u00a0 \u00a0 \u00a0 \u00a0 val result = bundle.getString(\"bundleKey\")\n\u00a0 \u00a0 \u00a0 \u00a0 \/\/ \u8655\u7406\u7d50\u679c...\n\u00a0 \u00a0 }\n}\n<\/pre>\n<p><strong>2. \u767c\u9001\u65b9 (ResultFragment)\uff1a\u5bc4\u51fa\u56de\u689d<\/strong> \u7528 setFragmentResult \u767c\u9001\u5e36\u6709\u540c\u6a23 requestKey \u7684\u7d50\u679c\u3002<\/p>\n<p>Kotlin<\/p>\n<pre class=\"wp-block-syntaxhighlighter-code\">\/\/ \u4e8b\u60c5\u8fa6\u5b8c\u5f8c...\nval result = bundleOf(\"bundleKey\" to \"some_result\")\nsetFragmentResult(\"requestKey\", result)\nparentFragmentManager.popBackStack()<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u53c3\u8003\u539f\u6587 \u8acb\u5fd8\u6389 getActivity() \u6216 findFragmentByTag()\u3002 \u73fe\u4ee3\u5316\u7684 Fragment \u901a\u8a0a\uff0c\u6838\u5fc3\u662f\u300c\u4e2d\u4ecb\u8005\u6a21\u5f0f\u300d\u3002\u4f60\u6709\u5169\u7a2e\u6b66\u5668\uff1aViewModel \u548c Fragmen&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[81],"tags":[],"_links":{"self":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/772"}],"collection":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=772"}],"version-history":[{"count":7,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/772\/revisions"}],"predecessor-version":[{"id":798,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/772\/revisions\/798"}],"wp:attachment":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=772"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=772"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=772"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}