{"id":221,"date":"2022-01-25T21:28:54","date_gmt":"2022-01-25T13:28:54","guid":{"rendered":"https:\/\/www.guger.top\/?p=221"},"modified":"2022-04-27T15:21:46","modified_gmt":"2022-04-27T07:21:46","slug":"android%e4%b8%ad%e5%85%b3%e4%ba%8eactivity%e4%b8%ad%e4%bc%a0%e9%80%92%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/www.guger.top\/index.php\/2022\/01\/25\/android%e4%b8%ad%e5%85%b3%e4%ba%8eactivity%e4%b8%ad%e4%bc%a0%e9%80%92%e6%95%b0%e6%8d%ae\/","title":{"rendered":"Android\u4e2d\u5173\u4e8eActivity\u4e2d\u4f20\u9012\u6570\u636e"},"content":{"rendered":"<p>\u6700\u8fd1\u60f3\u7ee7\u7eed\u81ea\u5df1\u4e4b\u524d\u7684\u68a6\u60f3\uff0c\u5165\u4e86\u5b89\u5353\u7f16\u7a0b\u7684\u5751\u9879\u76ee\u95ee\u9898\u7b49\u505a\u5b8c\u4e86\u6162\u6162\u590d\u76d8\uff0c\u5148\u603b\u7ed3\u4eca\u5929\u9047\u5230\u7684\u96be\u9898\u3002\u5982\u4f55\u5728Activity\u4e2d\u4f20\u9012\u6570\u636e\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u6570\u636e\u800c\u8a00\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7putExtra\u6765\u8fdb\u884c\u6570\u636e\u4f20\u9001<\/p>\n<pre><code class=\"language-koltin\">val data = \"Hello SecondActivity\"\nval intent = Intent(this, SecondActivity::class.java)\nintent.putExtra(\"extra_data\", data)\nstartActivity(intent)<\/code><\/code><\/pre>\n<p>\u4f46\u662f\u5bf9\u4e8e\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u800c\u8a00 putExtra\u65b9\u6cd5\u5c31\u4e0d\u5b58\u5728\u91cd\u6784\u65b9\u6cd5\u4e86\uff0c\u9700\u8981\u81ea\u5df1\u5b8c\u6210\u3002\u901a\u8fc7\u67e5\u9605\u8d44\u6599\u53d1\u73b0\u5927\u5bb6\u90fd\u5728\u91c7\u7528Parcelable\u7684\u7ba1\u9053\u7b26\u6765\u8fdb\u884c\u6570\u636e\u4f20\u8f93\u7684\u3002\u6bcf\u4e2aActivity\u76f8\u5f53\u4e8e\u72ec\u7acb\u7684\u5b58\u5728\uff0c\u672c\u8eab\u7684\u6570\u636e\u901a\u4fe1\u53ea\u80fd\u5728\u5185\u5b58\u4e2d\u53e6\u5916\u5f00\u8f9f\u4e00\u6bb5\u7a7a\u95f4\u8ba9Intet\u7c7b\u643a\u5e26\u6570\u636e\u8dd1\u5230\u53e6\u4e00\u4e2aActivity\u4e2d\u3002\u5982\u4f55\u4f7f\u7528Parcelable\u7ba1\u9053\u65b9\u6cd5\u5462\u3002\u76ee\u524d\u6211\u7684\u6570\u636e\u7ed3\u6784\u662f\u8fd9\u6837\u7684<\/p>\n<pre><code class=\"language-koltin\">class Fridge(val name: String? =&quot;&quot;, val location:String?=&quot;&quot;, val time:String?=&quot;&quot;)\nclass SearchJson(val response:Int, var data: List&lt;Fridge&gt;)<\/code><\/pre>\n<p>\u672c\u8eabSearchJson\u662f\u7528\u6765\u63a5\u6536json\u8fd4\u56de\u7684\u6570\u636e\u7528\u7684\uff0c\u8fd9\u91cc\u672c\u8eab\u6570\u636e\u4e3b\u8981\u662f\u540e\u9762\u7684data\u90e8\u5206\u6240\u4ee5\u5c31\u5168\u90e8\u4f20\u9012\u4e86\u3002\u76f4\u63a5\u4f7f\u7528putExtra\u662f\u65e0\u6cd5\u4f20\u9012\u7684\u3002\u672c\u8eab\u6ca1\u6709\u91cd\u8f7d\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u5c1d\u8bd5\u4f7f\u7528ArrayList\u7ed3\u6784\u4e5f\u4e0d\u884c\uff0c\u62f7\u8d1d\u65f6\u4f1a\u76f4\u63a5\u95ea\u9000\u3002\u867d\u7136\u8bf4\u662f\u7ee7\u627f\u5173\u7cfb\uff0c\u4f46\u662f\u8fd8\u662f\u4e0d\u80fd\u76f4\u63a5\u62f7\u8d1d\u3002\u6240\u4ee5\u5c31\u91c7\u7528Parcelable\u65b9\u6cd5\u4f20\u9012\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u76f8\u5f53\u4e8e\u628a\u6574\u4e2a\u7ed3\u6784\u8fdb\u884c\u4e86\u6d41\u5f0f\u6392\u5217\uff0c\u5c06\u6574\u4e2a\u7ed3\u6784\u6309\u7167\u4e00\u5b9a\u987a\u5e8f\u538b\u5165\u961f\u5217\u7136\u540e\u8fdb\u884c\u7ebf\u6027\u53d1\u9001\uff0c\u6240\u4ee5\u628a\u8fd9\u4e2a\u7c7b\u4f5c\u4e3a\u7236\u7c7b\uff0c\u7136\u540e\u5c31\u9700\u8981\u91cd\u8f7d\u4e94\u4e2a\u51fd\u6570<\/p>\n<pre><code class=\"language-koltin\">constructor(parcel: Parcel) :this\noverride fun writeToParcel(p0: Parcel?, p1: Int)\n override fun describeContents(): Int\n companion object CREATOR : Parcelable.Creator&lt;SearchJson&gt; {\n        override fun createFromParcel(parcel: Parcel): SearchJson {\n            return SearchJson(parcel)\n        }\n\n        override fun newArray(size: Int): Array&lt;SearchJson?&gt; {\n            return arrayOfNulls(size)\n        }<\/code><\/pre>\n<p>\u8fd9\u4e94\u4e2a\u65b9\u6cd5\u4e3b\u8981\u529f\u80fd\u662f\u524d\u4e24\u4e2a\u5206\u522b\u662f\u53cd\u5e8f\u5217\u5316\u548c\u5e8f\u5217\u5316\u3002\u7b80\u5355\u6765\u8bf4\u5c31\u662f\u6253\u5305\u51fd\u6570\u548c\u89e3\u538b\u51fd\u6570\u5982\u679c\u53ea\u662f\u7b80\u5355\u5143\u7d20\uff0cIDE\u4f1a\u5e2e\u4f60\u81ea\u52a8\u8865\u5168\u7684\uff0c\u57fa\u672c\u4e0d\u7528\u64cd\u5fc3\uff0c\u4f46\u662f\u6709\u5217\u8868\u5c31\u9700\u8981\u81ea\u5df1\u5199\u4e00\u4e0b\uff0c\u8fd9\u91cc\u6211\u4e5f\u662f\u521d\u4e0a\u624b\u8bd5\u56fe\u89e3\u51b3\u4e00\u4e0b\u8fd9\u51e0\u4e2a\u95ee\u9898<br \/>\nparcel\u63d0\u4f9b\u4e86\u5e8f\u5217\u5316\uff08writexxx\uff09\u548c\u53cd\u5e8f\u5217\u5316\uff08readxxx\uff09\u6765\u8fdb\u884c\u64cd\u4f5c\u3002\u4e00\u822c\u7684\u5982writeint\u5c31\u53ef\u4ee5\u628aint\u578b\u5199\u5165\u3002\u5982\u679c\u6d89\u53ca\u5230list\u578b\u4e5f\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528writelist \u4e0d\u8fc7\u8fd9\u91cc\u4e3b\u610f\uff0c\u5bf9\u4e8elist\u662f\u5176\u4ed6\u7c7b\u7684\u5217\u8868\uff0c\u5176\u4ed6\u7c7b\u4e5f\u9700\u8981\u7ee7\u627fParcelable\u7c7b\u5199\u76f8\u5e94\u7684\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u51fd\u6570\u6765\u5e2e\u52a9\u6253\u5305\u3002<\/p>\n<pre><code class=\"language-koltin\">override fun writeToParcel(p0: Parcel?, p1: Int) {\n        p0?.writeInt(response)\n        if(data.isNotEmpty())\n        {\n            p0?.writeInt(data.size)\n            p0?.writeList(data)\n        }\n\n        else\n        {\n            p0?.writeInt(0)\n        }<\/code><\/pre>\n<p>\u8fd9\u8fb9\u662f\u6211\u5199\u7684\u6253\u5305\u51fd\u6570\uff0c\u5bf9\u4e8edata\u800c\u8a00\u672c\u8eab\u662fList<Fridge>\u578b\uff0c\u53ea\u9700\u8981\u628aFridge\u4e5f\u505a\u76f8\u5e94\u5904\u7406\u5373\u53ef<\/p>\n<pre><code class=\"language-koltin\">override fun writeToParcel(parcel: Parcel, flags: Int) {\n        parcel.writeString(name)\n        parcel.writeString(location)\n        parcel.writeString(time)\n    }<\/code><\/pre>\n<p>\u8fd9\u6837\u6253\u5305\u90e8\u5206\u5c31\u7ed3\u675f\u4e86<\/p>\n<p>\u63a5\u4e0b\u6765\u662f \u53cd\u5e8f\u5217\u5316\u90e8\u5206\u3002\u5f53\u8fdb\u5165\u540e\u8fdb\u884c\u89e3\u5305\u5148\u8d34\u4ee3\u7801<\/p>\n<pre><code class=\"language-koltin\">constructor(parcel: Parcel) : this(response=parcel.readInt(),data= ArrayList&lt;Fridge&gt;()) {\n        if(parcel.readInt()&gt;0)\n        {\n            parcel.readList(data,Fridge.javaClass.classLoader)\n        }\n\n    }<\/code><\/pre>\n<p>\u6211\u8fd9\u91cc\u4e3b\u8981\u662f\u52a0\u4e86\u4e2a\u6821\u9a8c\u5982\u679c\u4e0d\u52a0\u6821\u9a8c\u4e5f\u662f\u53ef\u4ee5\u7684\u3002\u5728this\u6307\u9488\u90e8\u5206\u5148\u5bf9\u7c7b\u7684\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\u3002\u7136\u540e\u518d\u8d4b\u503c\uff0c\u5982\u679c\u662f\u57fa\u7840\u7c7b\u578b\u5982response\u53ef\u4ee5\u76f4\u63a5\u8d4b\u503c parcel.readInt() \u5bf9\u4e8e\u590d\u6742\u7c7b\u578b\u5148\u521d\u59cb\u5316ArrayList \u6ce8\u610fArrayList\u662f\u53ef\u53d8\u6570\u7ec4\uff0c\u4e0d\u5b58\u5728list\u7684\u521d\u59cb\u5316\u64cd\u4f5c\u3002\u4e0b\u9762\u4f7f\u7528\u7684\u662freadList\u51fd\u6570\uff0c\u5982\u679cwrite\u90e8\u5206\u4f7f\u7528writearraylist\u8fd9\u91cc\u5c31\u4e5f\u8981\u4e00\u81f4\u3002\u4e24\u8fb9\u4e0d\u4e00\u81f4\u4f1a\u5bfc\u81f4\u89e3\u6790\u51fa\u9519\u3002readlist\u7684\u4e24\u4e2a\u53c2\u6570\u5206\u522b\u662f\u8981\u5199\u5165\u7684\u53d8\u91cf\uff0c\u548clist\u5185\u90e8\u7684\u7c7b\u578b\u3002\u6ce8\u610f\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f <strong>Fridge<\/strong>.javaClass.classLoader<\/p>\n<p>\u7b2c\u4e09\u90e8\u5206\u662f\u5916\u90e8\u4ee3\u7801\u8fd9\u90e8\u5206\u5c31\u7b80\u5355\u4e86<\/p>\n<pre><code class=\"language-koltin\">                        val intent = Intent(this@SearchActivity,SearchResultActivity::class.java)\n                        intent.putExtra(&quot;fridge_list&quot;,list)\n                        startActivity(intent)<\/code><\/pre>\n<p>\u8fd9\u90e8\u5206\u8ddf\u4e00\u822c\u7684\u6ca1\u4ec0\u4e48\u4e0d\u4e00\u6837 \u6709\u4e2a\u5c0f\u7ec6\u8282 java\u8bed\u8a00\u7684this\u6307\u9488\u662f\u7c7b\u3002\u5199\u4f5c<code><code>searchActivity.this<\/code>`<code> kotlin\u5199\u4f5c <\/code>`<code>this@SearchActivity<\/code><\/code><\/p>\n<pre><code class=\"language-koltin\"> val receive= intent.getParcelableExtra&lt;SearchJson&gt;(&quot;fridge_list&quot;)\n        val list= receive?.data<\/code><\/pre>\n<p>\u8fd9\u90e8\u5206\u4f7f\u7528Parcelable\u51fd\u6570\u6765\u8fdb\u884c\u89e3\u6790\u6ce8\u610f\u7c7b\u578b\u540d\u79f0\u3002<br \/>\nPS\uff1a\u73b0\u5b66\u73b0\u5356\u8fd8\u662f\u6709\u70b9\u96be\u5ea6\u7684<br \/>\n\u53c2\u8003<br \/>\n<a href=\"https:\/\/stackoverflow.com\/questions\/6300608\/how-to-pass-a-parcelable-object-that-contains-a-list-of-objects\" target=\"_blank\"  rel=\"nofollow\" >https:\/\/stackoverflow.com\/questions\/6300608\/how-to-pass-a-parcelable-object-that-contains-a-list-of-objects<\/a><br \/>\n<a href=\"https:\/\/developer.android.com\/reference\/android\/os\/Parcel#readList(java.util.List,%20java.lang.ClassLoader\" target=\"_blank\"  rel=\"nofollow\" >https:\/\/developer.android.com\/reference\/android\/os\/Parcel#readList(java.util.List,%20java.lang.ClassLoader<\/a>)<br \/>\n<a href=\"https:\/\/medium.com\/the-lazy-coders-journal\/easy-parcelable-in-kotlin-the-lazy-coders-way-9683122f4c00\" target=\"_blank\"  rel=\"nofollow\" >https:\/\/medium.com\/the-lazy-coders-journal\/easy-parcelable-in-kotlin-the-lazy-coders-way-9683122f4c00<\/a><br \/>\n\n<table id=\"tablepress-3\" class=\"tablepress tablepress-id-3\">\n<thead>\n<tr class=\"row-1\">\n\t<th class=\"column-1\">\u533a\u522b<\/th><th class=\"column-2\">Serializable<\/th><th class=\"column-3\">Parcelable<\/th>\n<\/tr>\n<\/thead>\n<tbody class=\"row-striping row-hover\">\n<tr class=\"row-2\">\n\t<td class=\"column-1\">\u6240\u5c5eAPI\u4e0d\u540c<\/td><td class=\"column-2\">\u5c5e\u4e8eJava API<\/td><td class=\"column-3\">\u5c5e\u4e8e Android SDK<\/td>\n<\/tr>\n<tr class=\"row-3\">\n\t<td class=\"column-1\">\u539f\u7406\u4e0d\u540c<\/td><td class=\"column-2\">Serializable \u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u9700\u8981\u5927\u91cf\u7684I\/O\u64cd\u4f5c<\/td><td class=\"column-3\">Parcleable\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u4e0d\u9700\u8981\u5927\u91cf\u7684I\/O\u64cd\u4f5c<\/td>\n<\/tr>\n<tr class=\"row-4\">\n\t<td class=\"column-1\">\u5f00\u9500\u4e0d\u540c<\/td><td class=\"column-2\">\u5f00\u9500\u5927<\/td><td class=\"column-3\">\u5f00\u9500\u5c0f<\/td>\n<\/tr>\n<tr class=\"row-5\">\n\t<td class=\"column-1\">\u6548\u7387\u4e0d\u540c<\/td><td class=\"column-2\">\u6548\u7387\u4f4e<\/td><td class=\"column-3\">\u6548\u7387\u9ad8<\/td>\n<\/tr>\n<tr class=\"row-6\">\n\t<td class=\"column-1\">\u4f7f\u7528\u573a\u666f\u4e0d\u540c<\/td><td class=\"column-2\">Serializable\u5e8f\u5217\u5316\u5230\u672c\u5730\u6216\u8005\u7f51\u7edc\u4f20\u8f93<\/td><td class=\"column-3\">Parcleable\u662f\u5185\u5b58\u5e8f\u5217\u5316<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<!-- #tablepress-3 from cache --><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u60f3\u7ee7\u7eed\u81ea\u5df1\u4e4b\u524d\u7684\u68a6\u60f3\uff0c\u5165\u4e86\u5b89\u5353\u7f16\u7a0b\u7684\u5751\u9879\u76ee\u95ee\u9898\u7b49\u505a\u5b8c\u4e86\u6162\u6162\u590d\u76d8\uff0c\u5148\u603b\u7ed3\u4eca\u5929\u9047\u5230\u7684\u96be\u9898\u3002\u5982\u4f55\u5728Activity\u4e2d\u4f20\u9012\u6570\u636e\u3002\u5bf9\u4e8e\u7b80 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":""},"categories":[33],"tags":[35,34,36,37],"class_list":["post-221","post","type-post","status-publish","format-standard","hentry","category-android","tag-java","tag-kotlin","tag-parcelable","tag-putextra"],"_links":{"self":[{"href":"https:\/\/www.guger.top\/index.php\/wp-json\/wp\/v2\/posts\/221","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.guger.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guger.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guger.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guger.top\/index.php\/wp-json\/wp\/v2\/comments?post=221"}],"version-history":[{"count":0,"href":"https:\/\/www.guger.top\/index.php\/wp-json\/wp\/v2\/posts\/221\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.guger.top\/index.php\/wp-json\/wp\/v2\/media?parent=221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guger.top\/index.php\/wp-json\/wp\/v2\/categories?post=221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guger.top\/index.php\/wp-json\/wp\/v2\/tags?post=221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}