Search code examples
androidstringparcelablespannablestring

How to write SpannableString to parcel?


In my application I have dialog which consists few SpannableString objects. Because this is dialog, I need to be able to store it when user leaves application and comes back to it later on.

But the problem is how can I write SpannableString into Parcel?


Solution

  • I actually found out some way to do it using already existing functions, although I am not sure if it works for all type of spans, or just for those that I am using.

    Inside write parcel methos you have to add

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        TextUtils.writeToParcel(mSpannableString, dest, flags);
    }
    

    and next in order to extract SpannableString out of Parcel you should use this

    public CustomConstructor(Parcel parcel) {
        mSpannableString = (SpannableString) TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
    }