{"id":1249,"date":"2023-03-18T09:41:13","date_gmt":"2023-03-18T01:41:13","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1249"},"modified":"2023-04-29T09:29:43","modified_gmt":"2023-04-29T01:29:43","slug":"basic-usage-of-arouter","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/18\/basic-usage-of-arouter\/","title":{"rendered":"ARouter\u89e3\u6790\u4e4b\u57fa\u672c\u4f7f\u7528"},"content":{"rendered":"<p>ARouter\u662f\u963f\u91cc\u5df4\u5df4\u5f00\u6e90\u7684Android\u5e73\u53f0\u4e2d\u5bf9\u9875\u9762\u3001\u670d\u52a1\u63d0\u4f9b\u8def\u7531\u529f\u80fd\u7684\u4e2d\u95f4\u4ef6\uff0c\u63d0\u5021\u7684\u662f\u7b80\u5355\u4e14\u591f\u7528\u3002<\/p>\n<p>Github\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/alibaba\/ARouter\">https:\/\/github.com\/alibaba\/ARouter<\/a><\/p>\n<p><!-- more --><\/p>\n<p>Google\u63d0\u4f9b\u7684\u539f\u58f0\u8def\u7531\u4e3b\u8981\u662f\u901a\u8fc7Intent\uff0c\u53ef\u4ee5\u5206\u6210\u663e\u793a\u548c\u9690\u5f0f\u4e24\u79cd\u3002\u663e\u793a\u7684\u65b9\u6848\u4f1a\u5bfc\u81f4\u7c7b\u4e4b\u95f4\u7684\u76f4\u63a5\u4f9d\u8d56\u95ee\u9898\uff0c\u8026\u5408\u4e25\u91cd\uff1b\u9690\u5f0fIntent\u9700\u8981\u7684\u914d\u7f6e\u6e05\u5355\u4e2d\u7edf\u4e00\u58f0\u660e\uff0c\u9996\u5148\u6709\u4e2a\u66b4\u9732\u7684\u95ee\u9898\uff0c\u53e6\u5916\u5728\u591a\u6a21\u5757\u5f00\u53d1\u4e2d\u534f\u4f5c\u4e5f\u6bd4\u8f83\u56f0\u96be\u3002\u53ea\u8981\u8c03\u7528startActivity\u540e\u9762\u7684\u73af\u8282\u6211\u4eec\u5c31\u65e0\u6cd5\u63a7\u5236\u4e86\uff0c\u5728\u51fa\u73b0\u9519\u8bef\u65f6\u65e0\u80fd\u4e3a\u529b\uff0c\u800cARouter\u53ef\u4ee5\u5728\u8df3\u8f6c\u8fc7\u7a0b\u4e2d\u8fdb\u884c\u62e6\u622a\uff0c\u51fa\u73b0\u9519\u8bef\u65f6\u53ef\u4ee5\u5b9e\u73b0\u964d\u7ea7\u7b56\u7565\u3002<\/p>\n<h3>ARouter\u7684\u4f18\u52bf<\/h3>\n<ul>\n<li>\u76f4\u63a5\u89e3\u6790URL\u8def\u7531\uff0c\u89e3\u6790\u53c2\u6570\u5e76\u8d4b\u503c<\/li>\n<li>\u652f\u6301\u591a\u6a21\u5757\u9879\u76ee<\/li>\n<li>\u652f\u6301InstantRun<\/li>\n<li>\u5141\u8bb8\u81ea\u5b9a\u4e49\u62e6\u622a\u5668<\/li>\n<li>ARouter\u53ef\u4ee5\u63d0\u4f9bIoC\u5bb9\u5668<\/li>\n<li>\u6620\u5c04\u5173\u7cfb\u81ea\u52a8\u6ce8\u518c<\/li>\n<li>\u7075\u6d3b\u7684\u964d\u7ea7\u7b56\u7565<\/li>\n<\/ul>\n<h3>ARouter\u914d\u7f6e<\/h3>\n<pre><code class=\"language-java\">android {\n    defaultConfig {\n        javaCompileOptions {\n            annotationProcessorOptions {\n                arguments = [AROUTER_MODULE_NAME: project.getName()]\n            }\n        }\n    }\n}\n\ndependencies {\n    implementation &#039;com.alibaba:arouter-api:1.4.0&#039;\n    annotationProcessor &#039;com.alibaba:arouter-compiler:1.2.1&#039;\n}<\/code><\/pre>\n<h3>ARouter\u521d\u59cb\u5316<\/h3>\n<pre><code class=\"language-java\">\/\/ \u6253\u5f00\u65e5\u5fd7\u5e76\u6253\u5370\u5806\u6808\nARouter.openLog();\n\/\/ \u8c03\u8bd5\u6a21\u5f0f\u4e0d\u662f\u5fc5\u987b\u5f00\u542f\uff0c\u4f46\u662f\u4e3a\u4e86\u9632\u6b62\u6709\u7528\u6237\u5f00\u542f\u4e86InstantRun\uff0c\u4f46\u662f\u5fd8\u4e86\u5f00\u8c03\u8bd5\u6a21\u5f0f\uff0c\u5bfc\u81f4\u65e0\u6cd5\u4f7f\u7528ARouter\n\/\/ \u5982\u679c\u4f7f\u7528\u4e86InstantRun\uff0c\u5fc5\u987b\u5728\u521d\u59cb\u5316\u4e4b\u524d\u5f00\u542f\u8c03\u8bd5\u6a21\u5f0f\uff0c\u4f46\u662f\u4e0a\u7ebf\u524d\u9700\u8981\u5173\u95ed\n\/\/ InstantRun\u4ec5\u7528\u4e8e\u5f00\u53d1\u9636\u6bb5\uff0c\u7ebf\u4e0a\u5f00\u542f\u8c03\u8bd5\u6a21\u5f0f\u6709\u5b89\u5168\u98ce\u9669\uff0c\u53ef\u4ee5\u4f7f\u7528BuildConfig.DEBUG\u6765\u533a\u5206\u73af\u5883\nARouter.openDebug();\n\/\/ \u521d\u59cb\u5316ARouter\nARouter.init(getApplication());\n\/\/ \u5173\u95edARouter\n\/\/ ARouter.getInstance().destroy();<\/code><\/pre>\n<h3>Activity\u9875\u9762\u8df3\u8f6c\u53ca\u4f20\u53c2<\/h3>\n<pre><code class=\"language-java\">ARouter.getInstance()\n        .build(&quot;\/activity\/test&quot;)\n        .withString(&quot;name&quot;, &quot;Joe.Ye&quot;)\n        .withInt(&quot;age&quot;, 25)\n        .navigation();<\/code><\/pre>\n<h3>startActivityForResult<\/h3>\n<pre><code class=\"language-java\">ARouter.getInstance()\n        .build(&quot;\/activity\/test&quot;)\n        .withString(&quot;name&quot;, &quot;Joe.Ye&quot;)\n        .withInt(&quot;age&quot;, 25)\n        .navigation(this, REQUEST_CODE);<\/code><\/pre>\n<h3>\u8df3\u8f6c\u52a8\u753b<\/h3>\n<p>\uff081\uff09\u65e7\u7248\u52a8\u753b<\/p>\n<pre><code class=\"language-java\">ARouter.getInstance()\n        .build(&quot;\/activity\/test&quot;)\n        .withTransition(R.anim.slide_in_bottom, R.anim.slide_out_bottom)\n        .navigation(this);<\/code><\/pre>\n<p>\uff082\uff09\u65b0\u7248\u52a8\u753b<\/p>\n<pre><code class=\"language-java\">if (Build.VERSION.SDK_INT &gt;= 16) {\n    ActivityOptionsCompat compat = ActivityOptionsCompat.\n            makeScaleUpAnimation(v, v.getWidth() \/ 2, v.getHeight() \/ 2, 0, 0);\n\n    ARouter.getInstance()\n            .build(&quot;\/activity\/test&quot;)\n            .withOptionsCompat(compat)\n            .navigation();\n} else {\n    Toast.makeText(this, &quot;API &lt; 16,\u4e0d\u652f\u6301\u65b0\u7248\u672c\u52a8\u753b&quot;, Toast.LENGTH_SHORT).show();\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>ARouter\u662f\u963f\u91cc\u5df4\u5df4\u5f00\u6e90\u7684Android\u5e73\u53f0\u4e2d\u5bf9\u9875\u9762\u3001\u670d\u52a1\u63d0\u4f9b\u8def\u7531\u529f\u80fd\u7684\u4e2d\u95f4\u4ef6\uff0c\u63d0\u5021\u7684\u662f\u7b80\u5355\u4e14\u591f\u7528\u3002 Gi [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[115],"tags":[313],"class_list":["post-1249","post","type-post","status-publish","format-standard","hentry","category-thirdpart-framework","tag-arouter"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1249","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1249"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1249\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}