I want post an object with SpannableString by Intent. I let the object implement Serializable, but it will throw
java.io.NotSerializableException: android.text.SpannableString
So I let SpannableString implement Serializable.
private class MySpannableString extends SpannableString implements Serializable{
public MySpannableString(CharSequence source) {
super(source);
}
}
But I can't get the object in the next Activity.
As per the documentation for SpannableString, it does not implement the Serializable interface, hence you got the "java.io.NotSerializableException" exception.
Doc - https://developer.android.com/reference/android/text/SpannableString.html
Your approach to workaround this by extending SpannableString and implementing the Serializable interface will not work. The reason for this being that in order to make a non-serializable Class serializable by extending it, it needs to have an accessible no-argument constructor. The SpannableString class does not have that, the only accessible constructor that is has takes a CharacterSequence as an argument.
As per the documentation for the Serializable interface -
"To allow subtypes of non-serializable classes to be serialized, the subtype may assume responsibility for saving and restoring the state of the supertype's public, protected, and (if accessible) package fields. The subtype may assume this responsibility only if the class it extends has an accessible no-arg constructor to initialize the class's state. It is an error to declare a class Serializable if this is not the case. The error will be detected at runtime."
Doc - https://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html