{"action":"created","comment":{"url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/pulls/comments/27343608","id":27343608,"diff_hunk":"@@ -0,0 +1,570 @@\n+深入了解Bundle和Map\n+---\n+\n+>\n+* 原文链接 : [The mysterious case of the Bundle and the Map](https://medium.com/the-wtf-files/the-mysterious-case-of-the-bundle-and-the-map-7b15279a794e)\n+* 译者 : [yinna317](https://github.com/yinna317) \n+* 校对者: [这里校对者的github用户名](github链接)  \n+* 状态 :  寻找校对中\n+\n+\n+Because putting Maps in a Bundle is harder than it looks.\n+\n+[This post was written with the help of Eugenio @workingkills Marletti]\n+\n+Warning-this is a long post.\n+\n+\n+因为往Bundle对象中放入Map实际上没有表面上看起来那么容易\n+\n+这篇博客是在Eugenio @workingkills Marletti的帮助下写的\n+\n+警告：这是一篇篇幅较长的博客\n+\n+##When edge cases just aren’t covered\n+\n+Assume that you need to pass a map of values as an extra in an Intent. This might not be a common scenario, admittedly, but it can happen. It definitely happened to Eugenio.\n+\n+##案例：往Bundle对象放入特殊的Map\n+\n+假设有这样一个案例：你需要将一个要传递的map附加到Intent对象。这个案例虽然不常见，但是，这种情况也是很有可能发生。\n+`\n+\n+If you are using a HashMap, the most common type of Map, and you didn’t create a custom class that contains “extra” information, you’re lucky. You can put in:\n+\n+    \n+   intent.putExtra(\"map\", myHashMap);\n+\n+如果你在Intent对象中附加的是一个Map最常见的接口实现类HashMap，而不是包含附加信息的自定义类，你是幸运的，你可以用以下方法将map附加到Intent对象:\n+\n+And in your receiving Activity, you’ll get your nice map back out of the Intent’s extras:\n+\n+    HashMap map = (HashMap) getIntent().getSerializableExtra(\"map\");\n+\n+\n+在你接收的Activity里,你可以用以下方法毫无问题地取出之前在Intent中附加的Map\n+\n+\n+But what if you need to pass another kind of Map in the intent extras — say, a TreeMap (or any custom implementation)? Well, when you retrieve it:\n+\n+     TreeMap map = (TreeMap) getIntent().getSerializableExtra(\"map\");\n+\n+Then you get this:\n+\n+    java.lang.ClassCastException: java.util.HashMap cannot be cast to java.util.TreeMap\n+\n+但是，当你要向Intent对象附加另一种类型的Map,比如：一个TreeMap（或者其他的自定义Map接口实现类）,当你在Intent中取出之前附加的TreeMap时，你用如下方法：\n+\n+    ` TreeMap map = (TreeMap) getIntent().getSerializableExtra(\"map\");`\n+    \n+你获得一个类转换异常，因为你的Map（TreeMap）试图转换成一个HashMap\n+\n+We’ll see why we’re using getSerializableExtra() later on, suffice for now saying it’s because all the default Map implementations are Serializable and there’s no narrower scope of putExtra()/get*Extra() that can accept them.\n+\n+稍后我们将明白为什么我们用getSerializableExtra()这个方法来取出我们附加到Intent中的Map。从目前来看，是因为所有默认的Map接口实现类都是Serializable的（所有Map接口实现类都实现了Serializable接口），并且putExtra()/get*Extra()的作用域很广能够接受他们\n+\n+Before we move on, let’s get to know the actors involved in this drama.\n+\n+[tl:dr; skip to “Workaround” at the end if you just want a solution!]\n+\n+在我们进行下一步之前，让我们去了解参与这场戏的演员。（让我们去了解调用putExtra()/get*Extra()时涉及到的一些类或方法）\n+\n+提示：（文章很长，如果你只是想要一个解决方案的话，请跳转到文章的最后面解决方案那里）\n+\n+##On Parcels\n+\n+Many of you will know (but maybe some don’t) that all the IPC communication in the Android framework is based upon the concept of Binders. And hopefully many of you know that the main mechanism to allow data marshalling between those processes is based on Parcels.\n+\n+##Parcels:\n+\n+大家都知道（也许少部分人不知道），在Android 系统中所有进程间通信是基于Binder机制。但是，希望大家明白的是允许数据在进程间传递是基于Parcel.\n+\n+A Parcel is an optimised, non-general purpose serialisation mechanism that Android employs for IPC. Contrary to Serializable objects, you should never use Parcels for any kind of persistence, as it does not provision for handling different versions of the data. Whenever you see a Bundle, you’re dealing with a Parcel under the hood.\n+\n+        Adding extras to an Intent? Parcel.\n+\n+        Setting arguments on a Fragment? Parcel.\n+\n+        And so on.\n+\n+Parcel是Android进程间通信中, 高效的专用序列化机制。\n+Parcel与Serializable的区别之一是:Serializable可将数据持久化方便保存，（但是因为android不同版本Parcel可能不同），所以不推荐使用Parcel进行数据持久化. \n+\n+不管什么时候使用Bundle,你在底层处理的都是Parcel.比如附加数据到Intent对象,在Fragment中设参数，等等。","path":"androidweekly/深入了解Bundle和Map/readme.md","position":93,"original_position":93,"commit_id":"900db9eb77a9135f8f2760e17dba251721006b03","original_commit_id":"900db9eb77a9135f8f2760e17dba251721006b03","user":{"login":"yinna317","id":6448434,"avatar_url":"https://avatars.githubusercontent.com/u/6448434?v=3","gravatar_id":"","url":"https://api.github.com/users/yinna317","html_url":"https://github.com/yinna317","followers_url":"https://api.github.com/users/yinna317/followers","following_url":"https://api.github.com/users/yinna317/following{/other_user}","gists_url":"https://api.github.com/users/yinna317/gists{/gist_id}","starred_url":"https://api.github.com/users/yinna317/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yinna317/subscriptions","organizations_url":"https://api.github.com/users/yinna317/orgs","repos_url":"https://api.github.com/users/yinna317/repos","events_url":"https://api.github.com/users/yinna317/events{/privacy}","received_events_url":"https://api.github.com/users/yinna317/received_events","type":"User","site_admin":false},"body":"you should never use Parcels for any kind of persistence，翻译成Parcels 决不能被用于储存任何类型的持久性数据，是否太绝对化，翻译成绝不应该被用于储存任何类型的持久性数据是否好点。","created_at":"2015-03-28T09:26:35Z","updated_at":"2015-03-28T09:26:35Z","html_url":"https://github.com/bboyfeiyu/android-tech-frontier/pull/44#discussion_r27343608","pull_request_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/pulls/44","_links":{"self":{"href":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/pulls/comments/27343608"},"html":{"href":"https://github.com/bboyfeiyu/android-tech-frontier/pull/44#discussion_r27343608"},"pull_request":{"href":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/pulls/44"}}},"pull_request":{"url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/pulls/44","id":32140275,"html_url":"https://github.com/bboyfeiyu/android-tech-frontier/pull/44","diff_url":"https://github.com/bboyfeiyu/android-tech-frontier/pull/44.diff","patch_url":"https://github.com/bboyfeiyu/android-tech-frontier/pull/44.patch","issue_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/issues/44","number":44,"state":"open","locked":false,"title":"深入了解Bundle和Map","user":{"login":"yinna317","id":6448434,"avatar_url":"https://avatars.githubusercontent.com/u/6448434?v=3","gravatar_id":"","url":"https://api.github.com/users/yinna317","html_url":"https://github.com/yinna317","followers_url":"https://api.github.com/users/yinna317/followers","following_url":"https://api.github.com/users/yinna317/following{/other_user}","gists_url":"https://api.github.com/users/yinna317/gists{/gist_id}","starred_url":"https://api.github.com/users/yinna317/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yinna317/subscriptions","organizations_url":"https://api.github.com/users/yinna317/orgs","repos_url":"https://api.github.com/users/yinna317/repos","events_url":"https://api.github.com/users/yinna317/events{/privacy}","received_events_url":"https://api.github.com/users/yinna317/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-03-27T20:03:09Z","updated_at":"2015-03-28T09:26:35Z","closed_at":null,"merged_at":null,"merge_commit_sha":"b9fdf97ed033498a1d20925e73c902fd373b353a","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/pulls/44/commits","review_comments_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/pulls/44/comments","review_comment_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/pulls/comments{/number}","comments_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/issues/44/comments","statuses_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/statuses/900db9eb77a9135f8f2760e17dba251721006b03","head":{"label":"yinna317:master","ref":"master","sha":"900db9eb77a9135f8f2760e17dba251721006b03","user":{"login":"yinna317","id":6448434,"avatar_url":"https://avatars.githubusercontent.com/u/6448434?v=3","gravatar_id":"","url":"https://api.github.com/users/yinna317","html_url":"https://github.com/yinna317","followers_url":"https://api.github.com/users/yinna317/followers","following_url":"https://api.github.com/users/yinna317/following{/other_user}","gists_url":"https://api.github.com/users/yinna317/gists{/gist_id}","starred_url":"https://api.github.com/users/yinna317/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yinna317/subscriptions","organizations_url":"https://api.github.com/users/yinna317/orgs","repos_url":"https://api.github.com/users/yinna317/repos","events_url":"https://api.github.com/users/yinna317/events{/privacy}","received_events_url":"https://api.github.com/users/yinna317/received_events","type":"User","site_admin":false},"repo":{"id":32794988,"name":"android-tech-frontier","full_name":"yinna317/android-tech-frontier","owner":{"login":"yinna317","id":6448434,"avatar_url":"https://avatars.githubusercontent.com/u/6448434?v=3","gravatar_id":"","url":"https://api.github.com/users/yinna317","html_url":"https://github.com/yinna317","followers_url":"https://api.github.com/users/yinna317/followers","following_url":"https://api.github.com/users/yinna317/following{/other_user}","gists_url":"https://api.github.com/users/yinna317/gists{/gist_id}","starred_url":"https://api.github.com/users/yinna317/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yinna317/subscriptions","organizations_url":"https://api.github.com/users/yinna317/orgs","repos_url":"https://api.github.com/users/yinna317/repos","events_url":"https://api.github.com/users/yinna317/events{/privacy}","received_events_url":"https://api.github.com/users/yinna317/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/yinna317/android-tech-frontier","description":"一个定期翻译国外Android优质的技术、开源库、软件架构设计、测试等文章的开源项目","fork":true,"url":"https://api.github.com/repos/yinna317/android-tech-frontier","forks_url":"https://api.github.com/repos/yinna317/android-tech-frontier/forks","keys_url":"https://api.github.com/repos/yinna317/android-tech-frontier/keys{/key_id}","collaborators_url":"https://api.github.com/repos/yinna317/android-tech-frontier/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/yinna317/android-tech-frontier/teams","hooks_url":"https://api.github.com/repos/yinna317/android-tech-frontier/hooks","issue_events_url":"https://api.github.com/repos/yinna317/android-tech-frontier/issues/events{/number}","events_url":"https://api.github.com/repos/yinna317/android-tech-frontier/events","assignees_url":"https://api.github.com/repos/yinna317/android-tech-frontier/assignees{/user}","branches_url":"https://api.github.com/repos/yinna317/android-tech-frontier/branches{/branch}","tags_url":"https://api.github.com/repos/yinna317/android-tech-frontier/tags","blobs_url":"https://api.github.com/repos/yinna317/android-tech-frontier/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/yinna317/android-tech-frontier/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/yinna317/android-tech-frontier/git/refs{/sha}","trees_url":"https://api.github.com/repos/yinna317/android-tech-frontier/git/trees{/sha}","statuses_url":"https://api.github.com/repos/yinna317/android-tech-frontier/statuses/{sha}","languages_url":"https://api.github.com/repos/yinna317/android-tech-frontier/languages","stargazers_url":"https://api.github.com/repos/yinna317/android-tech-frontier/stargazers","contributors_url":"https://api.github.com/repos/yinna317/android-tech-frontier/contributors","subscribers_url":"https://api.github.com/repos/yinna317/android-tech-frontier/subscribers","subscription_url":"https://api.github.com/repos/yinna317/android-tech-frontier/subscription","commits_url":"https://api.github.com/repos/yinna317/android-tech-frontier/commits{/sha}","git_commits_url":"https://api.github.com/repos/yinna317/android-tech-frontier/git/commits{/sha}","comments_url":"https://api.github.com/repos/yinna317/android-tech-frontier/comments{/number}","issue_comment_url":"https://api.github.com/repos/yinna317/android-tech-frontier/issues/comments{/number}","contents_url":"https://api.github.com/repos/yinna317/android-tech-frontier/contents/{+path}","compare_url":"https://api.github.com/repos/yinna317/android-tech-frontier/compare/{base}...{head}","merges_url":"https://api.github.com/repos/yinna317/android-tech-frontier/merges","archive_url":"https://api.github.com/repos/yinna317/android-tech-frontier/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/yinna317/android-tech-frontier/downloads","issues_url":"https://api.github.com/repos/yinna317/android-tech-frontier/issues{/number}","pulls_url":"https://api.github.com/repos/yinna317/android-tech-frontier/pulls{/number}","milestones_url":"https://api.github.com/repos/yinna317/android-tech-frontier/milestones{/number}","notifications_url":"https://api.github.com/repos/yinna317/android-tech-frontier/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/yinna317/android-tech-frontier/labels{/name}","releases_url":"https://api.github.com/repos/yinna317/android-tech-frontier/releases{/id}","created_at":"2015-03-24T11:41:31Z","updated_at":"2015-03-24T10:58:50Z","pushed_at":"2015-03-27T19:57:43Z","git_url":"git://github.com/yinna317/android-tech-frontier.git","ssh_url":"git@github.com:yinna317/android-tech-frontier.git","clone_url":"https://github.com/yinna317/android-tech-frontier.git","svn_url":"https://github.com/yinna317/android-tech-frontier","homepage":"","size":15334,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"bboyfeiyu:master","ref":"master","sha":"bb4ffdf006bb1fb61edfbe4f2fc98cd3e94cbf34","user":{"login":"bboyfeiyu","id":1683811,"avatar_url":"https://avatars.githubusercontent.com/u/1683811?v=3","gravatar_id":"","url":"https://api.github.com/users/bboyfeiyu","html_url":"https://github.com/bboyfeiyu","followers_url":"https://api.github.com/users/bboyfeiyu/followers","following_url":"https://api.github.com/users/bboyfeiyu/following{/other_user}","gists_url":"https://api.github.com/users/bboyfeiyu/gists{/gist_id}","starred_url":"https://api.github.com/users/bboyfeiyu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bboyfeiyu/subscriptions","organizations_url":"https://api.github.com/users/bboyfeiyu/orgs","repos_url":"https://api.github.com/users/bboyfeiyu/repos","events_url":"https://api.github.com/users/bboyfeiyu/events{/privacy}","received_events_url":"https://api.github.com/users/bboyfeiyu/received_events","type":"User","site_admin":false},"repo":{"id":32553922,"name":"android-tech-frontier","full_name":"bboyfeiyu/android-tech-frontier","owner":{"login":"bboyfeiyu","id":1683811,"avatar_url":"https://avatars.githubusercontent.com/u/1683811?v=3","gravatar_id":"","url":"https://api.github.com/users/bboyfeiyu","html_url":"https://github.com/bboyfeiyu","followers_url":"https://api.github.com/users/bboyfeiyu/followers","following_url":"https://api.github.com/users/bboyfeiyu/following{/other_user}","gists_url":"https://api.github.com/users/bboyfeiyu/gists{/gist_id}","starred_url":"https://api.github.com/users/bboyfeiyu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bboyfeiyu/subscriptions","organizations_url":"https://api.github.com/users/bboyfeiyu/orgs","repos_url":"https://api.github.com/users/bboyfeiyu/repos","events_url":"https://api.github.com/users/bboyfeiyu/events{/privacy}","received_events_url":"https://api.github.com/users/bboyfeiyu/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/bboyfeiyu/android-tech-frontier","description":"一个定期翻译国外Android优质的技术、开源库、软件架构设计、测试等文章的开源项目","fork":false,"url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier","forks_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/forks","keys_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/keys{/key_id}","collaborators_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/teams","hooks_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/hooks","issue_events_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/issues/events{/number}","events_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/events","assignees_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/assignees{/user}","branches_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/branches{/branch}","tags_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/tags","blobs_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/git/refs{/sha}","trees_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/git/trees{/sha}","statuses_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/statuses/{sha}","languages_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/languages","stargazers_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/stargazers","contributors_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/contributors","subscribers_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/subscribers","subscription_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/subscription","commits_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/commits{/sha}","git_commits_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/git/commits{/sha}","comments_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/comments{/number}","issue_comment_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/issues/comments{/number}","contents_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/contents/{+path}","compare_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/compare/{base}...{head}","merges_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/merges","archive_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/downloads","issues_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/issues{/number}","pulls_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/pulls{/number}","milestones_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/milestones{/number}","notifications_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/labels{/name}","releases_url":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/releases{/id}","created_at":"2015-03-20T00:36:37Z","updated_at":"2015-03-28T09:05:32Z","pushed_at":"2015-03-28T05:09:51Z","git_url":"git://github.com/bboyfeiyu/android-tech-frontier.git","ssh_url":"git@github.com:bboyfeiyu/android-tech-frontier.git","clone_url":"https://github.com/bboyfeiyu/android-tech-frontier.git","svn_url":"https://github.com/bboyfeiyu/android-tech-frontier","homepage":"","size":16845,"stargazers_count":254,"watchers_count":254,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":69,"mirror_url":null,"open_issues_count":11,"forks":69,"open_issues":11,"watchers":254,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/pulls/44"},"html":{"href":"https://github.com/bboyfeiyu/android-tech-frontier/pull/44"},"issue":{"href":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/issues/44"},"comments":{"href":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/issues/44/comments"},"review_comments":{"href":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/pulls/44/comments"},"review_comment":{"href":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/pulls/44/commits"},"statuses":{"href":"https://api.github.com/repos/bboyfeiyu/android-tech-frontier/statuses/900db9eb77a9135f8f2760e17dba251721006b03"}}}}