How to properly write Parcelable implementation for an Map?











up vote
0
down vote

favorite
1












I haven't found a proper answer on the stackoverflow on the same and confused on how to achieve the right Parcelable implementation for an Map .



I suppose for a Map< String , String > below is the correct implementation:



public void writeToParcel(Parcel out, int flags){
out.writeInt(map.size());
for(Map.Entry<String,String> entry : map.entrySet()){
out.writeString(entry.getKey());
out.writeString(entry.getValue());
}
}

private MyParcelable(Parcel in){
//initialize your map before
int size = in.readInt();
for(int i = 0; i < size; i++){
String key = in.readString();
String value = in.readString();
map.put(key,value);
}
}


But what about Map < String , Object > ?










share|improve this question




























    up vote
    0
    down vote

    favorite
    1












    I haven't found a proper answer on the stackoverflow on the same and confused on how to achieve the right Parcelable implementation for an Map .



    I suppose for a Map< String , String > below is the correct implementation:



    public void writeToParcel(Parcel out, int flags){
    out.writeInt(map.size());
    for(Map.Entry<String,String> entry : map.entrySet()){
    out.writeString(entry.getKey());
    out.writeString(entry.getValue());
    }
    }

    private MyParcelable(Parcel in){
    //initialize your map before
    int size = in.readInt();
    for(int i = 0; i < size; i++){
    String key = in.readString();
    String value = in.readString();
    map.put(key,value);
    }
    }


    But what about Map < String , Object > ?










    share|improve this question


























      up vote
      0
      down vote

      favorite
      1









      up vote
      0
      down vote

      favorite
      1






      1





      I haven't found a proper answer on the stackoverflow on the same and confused on how to achieve the right Parcelable implementation for an Map .



      I suppose for a Map< String , String > below is the correct implementation:



      public void writeToParcel(Parcel out, int flags){
      out.writeInt(map.size());
      for(Map.Entry<String,String> entry : map.entrySet()){
      out.writeString(entry.getKey());
      out.writeString(entry.getValue());
      }
      }

      private MyParcelable(Parcel in){
      //initialize your map before
      int size = in.readInt();
      for(int i = 0; i < size; i++){
      String key = in.readString();
      String value = in.readString();
      map.put(key,value);
      }
      }


      But what about Map < String , Object > ?










      share|improve this question















      I haven't found a proper answer on the stackoverflow on the same and confused on how to achieve the right Parcelable implementation for an Map .



      I suppose for a Map< String , String > below is the correct implementation:



      public void writeToParcel(Parcel out, int flags){
      out.writeInt(map.size());
      for(Map.Entry<String,String> entry : map.entrySet()){
      out.writeString(entry.getKey());
      out.writeString(entry.getValue());
      }
      }

      private MyParcelable(Parcel in){
      //initialize your map before
      int size = in.readInt();
      for(int i = 0; i < size; i++){
      String key = in.readString();
      String value = in.readString();
      map.put(key,value);
      }
      }


      But what about Map < String , Object > ?







      android parcelable






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 7 at 14:53









      David Wasser

      67.3k10136198




      67.3k10136198










      asked Nov 7 at 13:42









      akshat tailang

      234




      234
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          You can't. Because you can't serialize an Object into a Parcel. If the Object in question implements Parcelable then you can do something like this:



          public void writeToParcel(Parcel out, int flags){
          out.writeInt(map.size());
          for(Map.Entry<String,Parcelable> entry : map.entrySet()){
          out.writeString(entry.getKey());
          entry.getValue().writeToParcel(out, flags);
          }
          }

          private MyParcelable(Parcel in){
          //initialize your map before
          int size = in.readInt();
          for(int i = 0; i < size; i++){
          String key = in.readString();
          Parcelable value = in.readParcelable(getClass().getClassLoader());
          map.put(key,value);
          }
          }


          HOWEVER: You don't need to do this, because the Parcel class already knows how to serialize and unserialize Map , provided that the key is a String and the value is a "known" object type (includes Serializable and Parcelable). So you can just do this:



          public void writeToParcel(Parcel out, int flags){
          out.writeValue(map);
          }


          private MyParcelable(Parcel in){
          map = (Map)in.readValue(getClass().getClassLoader());
          }


          See https://developer.android.com/reference/android/os/Parcel.html#writeValue(java.lang.Object) for a list of "known" object types.



          NOTE: You will get always get a HashMap if you call Parcel.getValue() on a Map. Android is stupid about this and assumes all Maps are HashMaps.






          share|improve this answer























          • Hi @David , thanks for the answer. i tried the first code snippet but it didn't work for me. Also i am confused on how to use the second snippet u shared, map = in.readValue(getClass().getClassLoader()); is not a valid statement according to android studio.
            – akshat tailang
            Nov 9 at 16:26










          • What do you mean by "didn't work". Be more specific please. Remote debugging is hard enough without details
            – David Wasser
            Nov 9 at 17:13










          • Reagding "not a valid statement", you probably need to cast the returned value. I've updated my answer.
            – David Wasser
            Nov 10 at 18:47











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53190655%2fhow-to-properly-write-parcelable-implementation-for-an-mapstring-object%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote













          You can't. Because you can't serialize an Object into a Parcel. If the Object in question implements Parcelable then you can do something like this:



          public void writeToParcel(Parcel out, int flags){
          out.writeInt(map.size());
          for(Map.Entry<String,Parcelable> entry : map.entrySet()){
          out.writeString(entry.getKey());
          entry.getValue().writeToParcel(out, flags);
          }
          }

          private MyParcelable(Parcel in){
          //initialize your map before
          int size = in.readInt();
          for(int i = 0; i < size; i++){
          String key = in.readString();
          Parcelable value = in.readParcelable(getClass().getClassLoader());
          map.put(key,value);
          }
          }


          HOWEVER: You don't need to do this, because the Parcel class already knows how to serialize and unserialize Map , provided that the key is a String and the value is a "known" object type (includes Serializable and Parcelable). So you can just do this:



          public void writeToParcel(Parcel out, int flags){
          out.writeValue(map);
          }


          private MyParcelable(Parcel in){
          map = (Map)in.readValue(getClass().getClassLoader());
          }


          See https://developer.android.com/reference/android/os/Parcel.html#writeValue(java.lang.Object) for a list of "known" object types.



          NOTE: You will get always get a HashMap if you call Parcel.getValue() on a Map. Android is stupid about this and assumes all Maps are HashMaps.






          share|improve this answer























          • Hi @David , thanks for the answer. i tried the first code snippet but it didn't work for me. Also i am confused on how to use the second snippet u shared, map = in.readValue(getClass().getClassLoader()); is not a valid statement according to android studio.
            – akshat tailang
            Nov 9 at 16:26










          • What do you mean by "didn't work". Be more specific please. Remote debugging is hard enough without details
            – David Wasser
            Nov 9 at 17:13










          • Reagding "not a valid statement", you probably need to cast the returned value. I've updated my answer.
            – David Wasser
            Nov 10 at 18:47















          up vote
          1
          down vote













          You can't. Because you can't serialize an Object into a Parcel. If the Object in question implements Parcelable then you can do something like this:



          public void writeToParcel(Parcel out, int flags){
          out.writeInt(map.size());
          for(Map.Entry<String,Parcelable> entry : map.entrySet()){
          out.writeString(entry.getKey());
          entry.getValue().writeToParcel(out, flags);
          }
          }

          private MyParcelable(Parcel in){
          //initialize your map before
          int size = in.readInt();
          for(int i = 0; i < size; i++){
          String key = in.readString();
          Parcelable value = in.readParcelable(getClass().getClassLoader());
          map.put(key,value);
          }
          }


          HOWEVER: You don't need to do this, because the Parcel class already knows how to serialize and unserialize Map , provided that the key is a String and the value is a "known" object type (includes Serializable and Parcelable). So you can just do this:



          public void writeToParcel(Parcel out, int flags){
          out.writeValue(map);
          }


          private MyParcelable(Parcel in){
          map = (Map)in.readValue(getClass().getClassLoader());
          }


          See https://developer.android.com/reference/android/os/Parcel.html#writeValue(java.lang.Object) for a list of "known" object types.



          NOTE: You will get always get a HashMap if you call Parcel.getValue() on a Map. Android is stupid about this and assumes all Maps are HashMaps.






          share|improve this answer























          • Hi @David , thanks for the answer. i tried the first code snippet but it didn't work for me. Also i am confused on how to use the second snippet u shared, map = in.readValue(getClass().getClassLoader()); is not a valid statement according to android studio.
            – akshat tailang
            Nov 9 at 16:26










          • What do you mean by "didn't work". Be more specific please. Remote debugging is hard enough without details
            – David Wasser
            Nov 9 at 17:13










          • Reagding "not a valid statement", you probably need to cast the returned value. I've updated my answer.
            – David Wasser
            Nov 10 at 18:47













          up vote
          1
          down vote










          up vote
          1
          down vote









          You can't. Because you can't serialize an Object into a Parcel. If the Object in question implements Parcelable then you can do something like this:



          public void writeToParcel(Parcel out, int flags){
          out.writeInt(map.size());
          for(Map.Entry<String,Parcelable> entry : map.entrySet()){
          out.writeString(entry.getKey());
          entry.getValue().writeToParcel(out, flags);
          }
          }

          private MyParcelable(Parcel in){
          //initialize your map before
          int size = in.readInt();
          for(int i = 0; i < size; i++){
          String key = in.readString();
          Parcelable value = in.readParcelable(getClass().getClassLoader());
          map.put(key,value);
          }
          }


          HOWEVER: You don't need to do this, because the Parcel class already knows how to serialize and unserialize Map , provided that the key is a String and the value is a "known" object type (includes Serializable and Parcelable). So you can just do this:



          public void writeToParcel(Parcel out, int flags){
          out.writeValue(map);
          }


          private MyParcelable(Parcel in){
          map = (Map)in.readValue(getClass().getClassLoader());
          }


          See https://developer.android.com/reference/android/os/Parcel.html#writeValue(java.lang.Object) for a list of "known" object types.



          NOTE: You will get always get a HashMap if you call Parcel.getValue() on a Map. Android is stupid about this and assumes all Maps are HashMaps.






          share|improve this answer














          You can't. Because you can't serialize an Object into a Parcel. If the Object in question implements Parcelable then you can do something like this:



          public void writeToParcel(Parcel out, int flags){
          out.writeInt(map.size());
          for(Map.Entry<String,Parcelable> entry : map.entrySet()){
          out.writeString(entry.getKey());
          entry.getValue().writeToParcel(out, flags);
          }
          }

          private MyParcelable(Parcel in){
          //initialize your map before
          int size = in.readInt();
          for(int i = 0; i < size; i++){
          String key = in.readString();
          Parcelable value = in.readParcelable(getClass().getClassLoader());
          map.put(key,value);
          }
          }


          HOWEVER: You don't need to do this, because the Parcel class already knows how to serialize and unserialize Map , provided that the key is a String and the value is a "known" object type (includes Serializable and Parcelable). So you can just do this:



          public void writeToParcel(Parcel out, int flags){
          out.writeValue(map);
          }


          private MyParcelable(Parcel in){
          map = (Map)in.readValue(getClass().getClassLoader());
          }


          See https://developer.android.com/reference/android/os/Parcel.html#writeValue(java.lang.Object) for a list of "known" object types.



          NOTE: You will get always get a HashMap if you call Parcel.getValue() on a Map. Android is stupid about this and assumes all Maps are HashMaps.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 10 at 18:48

























          answered Nov 7 at 14:52









          David Wasser

          67.3k10136198




          67.3k10136198












          • Hi @David , thanks for the answer. i tried the first code snippet but it didn't work for me. Also i am confused on how to use the second snippet u shared, map = in.readValue(getClass().getClassLoader()); is not a valid statement according to android studio.
            – akshat tailang
            Nov 9 at 16:26










          • What do you mean by "didn't work". Be more specific please. Remote debugging is hard enough without details
            – David Wasser
            Nov 9 at 17:13










          • Reagding "not a valid statement", you probably need to cast the returned value. I've updated my answer.
            – David Wasser
            Nov 10 at 18:47


















          • Hi @David , thanks for the answer. i tried the first code snippet but it didn't work for me. Also i am confused on how to use the second snippet u shared, map = in.readValue(getClass().getClassLoader()); is not a valid statement according to android studio.
            – akshat tailang
            Nov 9 at 16:26










          • What do you mean by "didn't work". Be more specific please. Remote debugging is hard enough without details
            – David Wasser
            Nov 9 at 17:13










          • Reagding "not a valid statement", you probably need to cast the returned value. I've updated my answer.
            – David Wasser
            Nov 10 at 18:47
















          Hi @David , thanks for the answer. i tried the first code snippet but it didn't work for me. Also i am confused on how to use the second snippet u shared, map = in.readValue(getClass().getClassLoader()); is not a valid statement according to android studio.
          – akshat tailang
          Nov 9 at 16:26




          Hi @David , thanks for the answer. i tried the first code snippet but it didn't work for me. Also i am confused on how to use the second snippet u shared, map = in.readValue(getClass().getClassLoader()); is not a valid statement according to android studio.
          – akshat tailang
          Nov 9 at 16:26












          What do you mean by "didn't work". Be more specific please. Remote debugging is hard enough without details
          – David Wasser
          Nov 9 at 17:13




          What do you mean by "didn't work". Be more specific please. Remote debugging is hard enough without details
          – David Wasser
          Nov 9 at 17:13












          Reagding "not a valid statement", you probably need to cast the returned value. I've updated my answer.
          – David Wasser
          Nov 10 at 18:47




          Reagding "not a valid statement", you probably need to cast the returned value. I've updated my answer.
          – David Wasser
          Nov 10 at 18:47


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53190655%2fhow-to-properly-write-parcelable-implementation-for-an-mapstring-object%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          The Sandy Post

          Danny Elfman

          Pages that link to "Head v. Amoskeag Manufacturing Co."