How to use the Parcelable class properly in Android

前端 未结 1 910
深忆病人
深忆病人 2021-01-22 05:15

I have a class (below) that I want to send to a Service Class through an Intent. I have implemented the Parcelable interface but am unsure how to actually send and retrieve the

相关标签:
1条回答
  • 2021-01-22 06:09

    In your writeToParcel function, you need to write which state objects you want to the Parcel, for instance:

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(urlPath);
        dest.writeString(hostname);
    }
    

    It doesn't matter which order you write the objects, so long as you read them back in in the same order:

    @Override
    public UrlParamsHelper(Parcel in) {
        urlPath = in.readString();
        hostname = in.readString();
    }
    

    The problem is that you can only read and write the object types mentioned in the documentation for Parcel, so it may be difficult to save absolutely everything.

    0 讨论(0)
提交回复
热议问题