How to parcelise member variable other than constructor in data class while using @Parcelize












0















I am using Room and Kotlin data class. Such as,



@Entity(tableName = "Person")
@Parcelize
class Test(@ColumnInfo(name = "name") var name:String) : Parcelable{
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "ID")
var id: Long? = null
}


I can create the instance using the constructor and insert the data. I am also getting a warning "property would not be serialized into a 'parcel'". When I was trying to send the object through a bundle, the id is missing, which is expected as the warning says so. How can I add that member ID in the parcel? I am not keeping the ID in the constructor as I want them to be generated automatically.



Thanks in Advance.










share|improve this question



























    0















    I am using Room and Kotlin data class. Such as,



    @Entity(tableName = "Person")
    @Parcelize
    class Test(@ColumnInfo(name = "name") var name:String) : Parcelable{
    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "ID")
    var id: Long? = null
    }


    I can create the instance using the constructor and insert the data. I am also getting a warning "property would not be serialized into a 'parcel'". When I was trying to send the object through a bundle, the id is missing, which is expected as the warning says so. How can I add that member ID in the parcel? I am not keeping the ID in the constructor as I want them to be generated automatically.



    Thanks in Advance.










    share|improve this question

























      0












      0








      0








      I am using Room and Kotlin data class. Such as,



      @Entity(tableName = "Person")
      @Parcelize
      class Test(@ColumnInfo(name = "name") var name:String) : Parcelable{
      @PrimaryKey(autoGenerate = true)
      @ColumnInfo(name = "ID")
      var id: Long? = null
      }


      I can create the instance using the constructor and insert the data. I am also getting a warning "property would not be serialized into a 'parcel'". When I was trying to send the object through a bundle, the id is missing, which is expected as the warning says so. How can I add that member ID in the parcel? I am not keeping the ID in the constructor as I want them to be generated automatically.



      Thanks in Advance.










      share|improve this question














      I am using Room and Kotlin data class. Such as,



      @Entity(tableName = "Person")
      @Parcelize
      class Test(@ColumnInfo(name = "name") var name:String) : Parcelable{
      @PrimaryKey(autoGenerate = true)
      @ColumnInfo(name = "ID")
      var id: Long? = null
      }


      I can create the instance using the constructor and insert the data. I am also getting a warning "property would not be serialized into a 'parcel'". When I was trying to send the object through a bundle, the id is missing, which is expected as the warning says so. How can I add that member ID in the parcel? I am not keeping the ID in the constructor as I want them to be generated automatically.



      Thanks in Advance.







      android serialization kotlin android-room






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 4:00









      sadatsadat

      661615




      661615
























          2 Answers
          2






          active

          oldest

          votes


















          0














          You can find this information with the documentation:




          @Parcelize requires all serialized properties to be declared in the primary constructor. Android Extensions will issue a warning on each property with a backing field declared in the class body. Also, @Parcelize can't be applied if some of the primary constructor parameters are not properties.



          If your class requires more advanced serialization logic, you can write it inside a companion class:




          @Parcelize
          data class User(val firstName: String, val lastName: String, val age: Int) : Parcelable {
          private companion object : Parceler<User> {
          override fun User.write(parcel: Parcel, flags: Int) {
          // Custom write implementation
          }

          override fun create(parcel: Parcel): User {
          // Custom read implementation
          }
          }
          }





          share|improve this answer































            -1














            Thanks @tynn for the reference. I am making another post in case someone can't figure out the workaround.



            @Entity(tableName = "Person")
            @Parcelize
            data class Test(@ColumnInfo(name = "name") var name:String,
            @PrimaryKey(autoGenerate = true)
            @ColumnInfo(name = "ID")
            var id: Long? = null) : Parcelable


            And you can still create an object without ID like Test("test name") and it will insert an incremented value when the object will be saved in ROOM.






            share|improve this answer























              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',
              autoActivateHeartbeat: false,
              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%2f53312247%2fhow-to-parcelise-member-variable-other-than-constructor-in-data-class-while-usin%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              You can find this information with the documentation:




              @Parcelize requires all serialized properties to be declared in the primary constructor. Android Extensions will issue a warning on each property with a backing field declared in the class body. Also, @Parcelize can't be applied if some of the primary constructor parameters are not properties.



              If your class requires more advanced serialization logic, you can write it inside a companion class:




              @Parcelize
              data class User(val firstName: String, val lastName: String, val age: Int) : Parcelable {
              private companion object : Parceler<User> {
              override fun User.write(parcel: Parcel, flags: Int) {
              // Custom write implementation
              }

              override fun create(parcel: Parcel): User {
              // Custom read implementation
              }
              }
              }





              share|improve this answer




























                0














                You can find this information with the documentation:




                @Parcelize requires all serialized properties to be declared in the primary constructor. Android Extensions will issue a warning on each property with a backing field declared in the class body. Also, @Parcelize can't be applied if some of the primary constructor parameters are not properties.



                If your class requires more advanced serialization logic, you can write it inside a companion class:




                @Parcelize
                data class User(val firstName: String, val lastName: String, val age: Int) : Parcelable {
                private companion object : Parceler<User> {
                override fun User.write(parcel: Parcel, flags: Int) {
                // Custom write implementation
                }

                override fun create(parcel: Parcel): User {
                // Custom read implementation
                }
                }
                }





                share|improve this answer


























                  0












                  0








                  0







                  You can find this information with the documentation:




                  @Parcelize requires all serialized properties to be declared in the primary constructor. Android Extensions will issue a warning on each property with a backing field declared in the class body. Also, @Parcelize can't be applied if some of the primary constructor parameters are not properties.



                  If your class requires more advanced serialization logic, you can write it inside a companion class:




                  @Parcelize
                  data class User(val firstName: String, val lastName: String, val age: Int) : Parcelable {
                  private companion object : Parceler<User> {
                  override fun User.write(parcel: Parcel, flags: Int) {
                  // Custom write implementation
                  }

                  override fun create(parcel: Parcel): User {
                  // Custom read implementation
                  }
                  }
                  }





                  share|improve this answer













                  You can find this information with the documentation:




                  @Parcelize requires all serialized properties to be declared in the primary constructor. Android Extensions will issue a warning on each property with a backing field declared in the class body. Also, @Parcelize can't be applied if some of the primary constructor parameters are not properties.



                  If your class requires more advanced serialization logic, you can write it inside a companion class:




                  @Parcelize
                  data class User(val firstName: String, val lastName: String, val age: Int) : Parcelable {
                  private companion object : Parceler<User> {
                  override fun User.write(parcel: Parcel, flags: Int) {
                  // Custom write implementation
                  }

                  override fun create(parcel: Parcel): User {
                  // Custom read implementation
                  }
                  }
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 7:45









                  tynntynn

                  20.5k54782




                  20.5k54782

























                      -1














                      Thanks @tynn for the reference. I am making another post in case someone can't figure out the workaround.



                      @Entity(tableName = "Person")
                      @Parcelize
                      data class Test(@ColumnInfo(name = "name") var name:String,
                      @PrimaryKey(autoGenerate = true)
                      @ColumnInfo(name = "ID")
                      var id: Long? = null) : Parcelable


                      And you can still create an object without ID like Test("test name") and it will insert an incremented value when the object will be saved in ROOM.






                      share|improve this answer




























                        -1














                        Thanks @tynn for the reference. I am making another post in case someone can't figure out the workaround.



                        @Entity(tableName = "Person")
                        @Parcelize
                        data class Test(@ColumnInfo(name = "name") var name:String,
                        @PrimaryKey(autoGenerate = true)
                        @ColumnInfo(name = "ID")
                        var id: Long? = null) : Parcelable


                        And you can still create an object without ID like Test("test name") and it will insert an incremented value when the object will be saved in ROOM.






                        share|improve this answer


























                          -1












                          -1








                          -1







                          Thanks @tynn for the reference. I am making another post in case someone can't figure out the workaround.



                          @Entity(tableName = "Person")
                          @Parcelize
                          data class Test(@ColumnInfo(name = "name") var name:String,
                          @PrimaryKey(autoGenerate = true)
                          @ColumnInfo(name = "ID")
                          var id: Long? = null) : Parcelable


                          And you can still create an object without ID like Test("test name") and it will insert an incremented value when the object will be saved in ROOM.






                          share|improve this answer













                          Thanks @tynn for the reference. I am making another post in case someone can't figure out the workaround.



                          @Entity(tableName = "Person")
                          @Parcelize
                          data class Test(@ColumnInfo(name = "name") var name:String,
                          @PrimaryKey(autoGenerate = true)
                          @ColumnInfo(name = "ID")
                          var id: Long? = null) : Parcelable


                          And you can still create an object without ID like Test("test name") and it will insert an incremented value when the object will be saved in ROOM.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 15 '18 at 8:52









                          sadatsadat

                          661615




                          661615






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312247%2fhow-to-parcelise-member-variable-other-than-constructor-in-data-class-while-usin%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

                              Bressuire

                              Vorschmack

                              Quarantine