Reputation: 49
I need to understand if a transient variable inside a class that implements Parcelable interface should be read from the parcel in modelClass(Parcel parcel) method or written to the parcel in writeToParcel(Parcel parcel,int i) . Can anyone provide me with a class implementation with a transient variable in it. Thank you.
Upvotes: 1
Views: 994
Reputation: 3912
The "transient"-keyword has no effect on parcelable objects. There is no automation in reading and writing the fields in a parcelable object, so there is no ready made code that would take it into account. Any possible choice of special processing for transient fields is completely up to the person designing the class.
Specification (https://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.3.1.3) says "Variables may be marked transient to indicate that they are not part of the persistent state of an object" so if you really want to go by the book you should not write them. But, as I said, since the reading and writing is done mechanically inside the class, the transient keyword doesn't make much sense.
Upvotes: 1
Reputation:
You can simply add transient keyword before data type while declaring variable.
class Parcel implements Parcelable{
private Integer checkinId;
private transient String someCode;
//// some methods
}
interface Parcelable implements{
// some methods
}
Upvotes: 0
Reputation: 91
class Employee implements Serializable {
private String firstName;
private String lastName;
private transient String confidentialInfo;
//Setters and Getters
}
Upvotes: 0