How to parcelise member variable other than constructor in data class while using @Parcelize
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
add a comment |
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
add a comment |
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
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
android serialization kotlin android-room
asked Nov 15 '18 at 4:00
sadatsadat
661615
661615
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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
}
}
}
add a comment |
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
}
}
}
add a comment |
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
}
}
}
add a comment |
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
}
}
}
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
}
}
}
answered Nov 15 '18 at 7:45
tynntynn
20.5k54782
20.5k54782
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 15 '18 at 8:52
sadatsadat
661615
661615
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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