{"id":1067,"date":"2025-12-31T16:39:19","date_gmt":"2025-12-31T08:39:19","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=1067"},"modified":"2025-12-31T16:39:19","modified_gmt":"2025-12-31T08:39:19","slug":"startactivityforresult","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2025\/12\/31\/startactivityforresult\/","title":{"rendered":"startactivityforresult"},"content":{"rendered":"\n<p><code>startActivityForResult<\/code> \u662f <strong>Android \u65e9\u671f\u7528\u4e8e\u300c\u542f\u52a8 Activity \u5e76\u63a5\u6536\u8fd4\u56de\u7ed3\u679c\u300d\u7684\u65b9\u6cd5<\/strong>\uff0c<br>\u4f46 <strong>\u5df2\u5728 Android 11\uff08API 30\uff09\u8d77\u88ab\u5b98\u65b9\u6807\u8bb0\u4e3a\u5e9f\u5f03\uff08Deprecated\uff09<\/strong>\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u6211\u7ed9\u4f60 <strong>\u5b8c\u6574\u8bf4\u660e + \u65e7\u7528\u6cd5 + \u65b0\u5b98\u65b9\u63a8\u8350\u7528\u6cd5\uff08\u5fc5\u987b\u4f1a\uff09<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001<code>startActivityForResult<\/code> \u662f\u5e72\u561b\u7684\uff1f<\/h2>\n\n\n\n<p>\ud83d\udc49 <strong>\u542f\u52a8\u4e00\u4e2a Activity\uff0c\u5e76\u5728\u8fd4\u56de\u65f6\u62ff\u5230\u7ed3\u679c<\/strong><\/p>\n\n\n\n<p>\u5178\u578b\u573a\u666f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6253\u5f00\u76f8\u518c \u2192 \u8fd4\u56de\u56fe\u7247\u8def\u5f84<\/li>\n\n\n\n<li>\u6253\u5f00\u767b\u5f55\u9875 \u2192 \u8fd4\u56de\u767b\u5f55\u7ed3\u679c<\/li>\n\n\n\n<li>\u6253\u5f00\u8bbe\u7f6e\u9875 \u2192 \u8fd4\u56de\u7528\u6237\u9009\u62e9<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u65e7\u7528\u6cd5\uff08\u5df2\u5e9f\u5f03\uff0c\u4f46\u4f60\u4e00\u5b9a\u4f1a\u89c1\u5230\uff09<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1\ufe0f\u20e3 \u542f\u52a8 Activity<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nIntent intent = new Intent(this, SecondActivity.class);\nstartActivityForResult(intent, 1001);\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">2\ufe0f\u20e3 \u63a5\u6536\u8fd4\u56de\u7ed3\u679c<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n@Override\nprotected void onActivityResult(int requestCode, int resultCode, Intent data) {\n    super.onActivityResult(requestCode, resultCode, data);\n\n    if (requestCode == 1001 &amp;amp;&amp;amp; resultCode == RESULT_OK) {\n        String result = data.getStringExtra(&quot;result&quot;);\n    }\n}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">3\ufe0f\u20e3 \u8fd4\u56de\u7ed3\u679c\uff08SecondActivity\uff09<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nIntent intent = new Intent();\nintent.putExtra(&quot;result&quot;, &quot;Hello&quot;);\nsetResult(RESULT_OK, intent);\nfinish();\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u274c \u65e7\u65b9\u5f0f\u7684\u95ee\u9898<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>requestCode \u5bb9\u6613\u51b2\u7a81<\/li>\n\n\n\n<li>\u751f\u547d\u5468\u671f\u8026\u5408\u4e25\u91cd<\/li>\n\n\n\n<li>Fragment \u4f7f\u7528\u6781\u5176\u6df7\u4e71<\/li>\n\n\n\n<li>\u4e0d\u7b26\u5408\u73b0\u4ee3\u67b6\u6784\uff08ViewModel \/ Lifecycle\uff09<\/li>\n<\/ul>\n\n\n\n<p>\ud83d\udc49 <strong>\u6240\u4ee5\u88ab\u5b98\u65b9\u5e9f\u5f03<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u2705 \u5b98\u65b9\u63a8\u8350\uff1aActivity Result API\uff08\u5fc5\u987b\u638c\u63e1\uff09<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u2714 \u6838\u5fc3\u601d\u60f3<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5148\u6ce8\u518c\u56de\u8c03<\/strong><\/li>\n\n\n\n<li><strong>\u518d\u542f\u52a8 Activity<\/strong><\/li>\n\n\n\n<li>\u751f\u547d\u5468\u671f\u5b89\u5168<\/li>\n\n\n\n<li>Fragment \/ Activity \u901a\u7528<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u73b0\u4ee3\u6b63\u786e\u5199\u6cd5\uff08\u5f3a\u70c8\u63a8\u8350\uff09<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1\ufe0f\u20e3 \u6ce8\u518c\u7ed3\u679c\u56de\u8c03<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nActivityResultLauncher&amp;lt;Intent&gt; launcher =\n        registerForActivityResult(\n                new ActivityResultContracts.StartActivityForResult(),\n                result -&gt; {\n                    if (result.getResultCode() == Activity.RESULT_OK) {\n                        Intent data = result.getData();\n                        if (data != null) {\n                            String value = data.getStringExtra(&quot;result&quot;);\n                        }\n                    }\n                }\n        );\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">2\ufe0f\u20e3 \u542f\u52a8 Activity<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nIntent intent = new Intent(this, SecondActivity.class);\nlauncher.launch(intent);\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">3\ufe0f\u20e3 \u8fd4\u56de\u7ed3\u679c\uff08\u548c\u4ee5\u524d\u4e00\u6837\uff09<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nIntent intent = new Intent();\nintent.putExtra(&quot;result&quot;, &quot;Hello&quot;);\nsetResult(Activity.RESULT_OK, intent);\nfinish();\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001Kotlin \u7248\uff08\u73b0\u5728\u6700\u5e38\u89c1\uff09<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nprivate val launcher =\n    registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {\n        if (it.resultCode == Activity.RESULT_OK) {\n            val result = it.data?.getStringExtra(&quot;result&quot;)\n        }\n    }\n\n\/\/ \u542f\u52a8\nlauncher.launch(Intent(this, SecondActivity::class.java))\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u5e38\u7528 ActivityResultContracts<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Contract<\/th><th>\u7528\u9014<\/th><\/tr><\/thead><tbody><tr><td><code>StartActivityForResult<\/code><\/td><td>\u542f\u52a8\u666e\u901a Activity<\/td><\/tr><tr><td><code>TakePicture<\/code><\/td><td>\u62cd\u7167<\/td><\/tr><tr><td><code>PickVisualMedia<\/code><\/td><td>\u9009\u56fe\u7247\uff08\u63a8\u8350\uff09<\/td><\/tr><tr><td><code>RequestPermission<\/code><\/td><td>\u8bf7\u6c42\u5355\u4e2a\u6743\u9650<\/td><\/tr><tr><td><code>RequestMultiplePermissions<\/code><\/td><td>\u591a\u6743\u9650<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e03\u3001\u4ec0\u4e48\u65f6\u5019\u8fd8\u80fd\u770b\u5230 <code>startActivityForResult<\/code>\uff1f<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8001\u9879\u76ee<\/li>\n\n\n\n<li>\u8001\u6559\u7a0b<\/li>\n\n\n\n<li>Android 8 \/ 9 \u9879\u76ee<\/li>\n\n\n\n<li>\u516c\u53f8\u5386\u53f2\u4ee3\u7801\uff08\u975e\u5e38\u591a \ud83d\ude05\uff09<\/li>\n<\/ul>\n\n\n\n<p>\ud83d\udc49 <strong>\u65b0\u9879\u76ee\u3001\u65b0\u529f\u80fd\u4e00\u5f8b\u4e0d\u8981\u518d\u7528<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516b\u3001\u603b\u7ed3\u4e00\u53e5\u8bdd<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong><code>startActivityForResult<\/code> \u5df2\u6b7b\uff0cActivity Result API \u662f\u672a\u6765<\/strong><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>startActivityForResult \u662f Android \u65e9\u671f\u7528\u4e8e\u300c\u542f\u52a8&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/12\/31\/startactivityforresult\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-1067","post","type-post","status-publish","format-standard","hentry","category-12"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/1067","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=1067"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/1067\/revisions"}],"predecessor-version":[{"id":1068,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/1067\/revisions\/1068"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=1067"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=1067"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=1067"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}