I've got data class that contains variable of type PagedList?. This class needs to implement Parcelable because I want to save its state inside my Bundle.
How can I parcel objects of this type? #Parcelize doesn't help. How can I write a custom Parceler for PagedList? Or maybe there's another way by some kind of object wrapping?
You just have to give annotation #Parcelize on the class and extend that class to Parcelable and if any property of that class having Another class then that class also should be Parcelable.
And if you getting some error or something wrong then please let me know in comments.
Related
I have a Result parcelable class that is supposed to serve as a container for a key and a parcelable data. This is how I define it:
#Parcelize
open class Result<out T : Parcelable>(val key: String, val data: T?) : Parcelable
The problem is that when defining the child class, in order for #Parcelize to work, I need to add val to the object in the constructor, essentially making the data being written twice to the parcel twice. Here is an example:
#Parcelize
class LessonFinishedResult(private val lessonActivityData: LessonActivityData) :
Result<LessonActivityData>(LessonActivityData.LESSON_KEY, lessonActivityData), Parcelable
I would like to have this:
#Parcelize
class LessonFinishedResult(lessonActivityData: LessonActivityData) :
Result<LessonActivityData>(LessonActivityData.LESSON_KEY, lessonActivityData), Parcelable
But it is not allowed. Is there a smart way I can get around this? And another thing, is there a way I can avoid having to manually use Result<LessonActivityData> and have the type be inferred automatically from the object being passed?
Thanks!
essentially making the data being written twice to the parcel twice
It isn't; the code generated by #Parcelize doesn't care about the superclass at all. It can even not be Parcelizable. Only the primary constructor parameters (in this case val lessonActivityData) are written/read, and when reading it's enough to pass the correct parameters to the superclass constructor.
I'm trying to pass an Object from one activity to another and I know I should use Parcelable or Serializable but my Object class implements an interface already. Is there any way around this?
Objects can implement multiple interfaces:
class MyClass implements Interface1, Parcelable {
// Implement each interface
}
I think the right thing is just to use Intent.putExtras() - where you can pass primitive data types + objects of type String, Bundle, Parcelable, Serializable. You are simply using key/value pairs. And after that you can get your data by Intent.getExtras(). Everything is quite simple. Also have a look at this links, they are for bigginers, but really helpful: http://developer.android.com/guide/components/intents-filters.html and http://www.vogella.com/tutorials/AndroidIntent/article.html. If the problem is somewhere deeper - please describe it. Thanks.
Sure! A class can implement multiple interfaces. You'll just need to separate each one with a comma in your class declaration, like this...
public class YourClass implements interface1, interface2, interface3 {
//...
}
An object cannot extend more than one class but can implement many interfaces.
Parcelable and `Serializable` are the way but little complex. an easy solution is just use `Gson` or any other JSON library..
in first activity.
String objJson = new Gson().toJson(object);
intent.putExtra("key",objJson);
and in your second activity
YourClass yourClass = new Gson().fromJson(getIntent().getStringExtra("key"),YourClass.class) ;
for Gson library check this link http://www.java2s.com/Code/Jar/g/Downloadgson222jar.htm
I have an object that i must save to file for reuse. The class of this object already implements Parcelable for use in intents. My knowledge of saving an object to file says to implement Serializable, but when i do, i get an error in the class that contains this object at the putExtra method of an intent because both Serializable and Parcelable have this method.
Is there a way to avoid this, or just a way that i can save my object state and reload it easily?
I have looked at a few articles and i feel no more informed about how i should be saving my object.
Thanks in advance
I believe that Parcelable and Serializable both reaches the same goal in different ways and with different performances. Given that, if some class in your object hierarchy alread implements the Parcelable interface, you can override its writeToParcel method, call the super for it (so the members of the super classes will be written to the parcel if they were implement that way) and then, you should write your attributes to the parcel, always keeping in mind that the order you use to save them is the order you will use to retrieve them latter (FILO data structure)
EDIT
Just cast your object where it complains and tells about the conflict to the class you want to use as described here: https://stackoverflow.com/a/13880819/2068693
I don't know that you can implement both Serializable and Parcelable together but for convert a class from Serializable to Parcelable you can use this plugin:
Android Parcelable Code generator.
First remove implement Serializable then with ALT + Insert and click on Parcelable you can generate your class.
You have options other than Serializable, but that may meet other requirements such as avoiding library dependencies. You can write objects to file using JSON or XML, which has the advantage of being readable. You may also need to consider versioning - what happens when you have files that need to be read by a class that contains a new field. Persistence brings with it some issues you probably don't have passing Bundles/Intents back and forth.
If you choose Serializable I'd recommend structuring your objects so they can be written to and read from a Bundle. Using a static MyObject.make(Bundle) method and an instance Bundle save() method keeps all the constants and read/write in a single location.
I have a class that extends Parcelable, we'll call it Class A.
I have another class that extends Parcelable, we'll call it Class B.
One of the member variables of A is an ArrayList of Class B objects.
I'm trying to write the Parcelable overrides, but can't figure out how to save and read the ArrayList of Class B.
There are methods to read and write ArrayLists, but they want a parameter which is a ClassLoader and I am unfamiliar with it. I could also copy the ArrayList into an array and user the methods to read and write a Parcelable array, but this also requires a ClassLoader parameter.
Update:
It looks like
in.readTypedList(mList, ClassB.CREATOR);
and
out.writeTypedList(mList);
are what I'm looking for?
You can pass null as ClassLoader to use the default one.
Parcel.readTypedList() and Parcel.writeTypedList() seem to be OK. Or you can use Parcel.readParcelableArray() and Parcel.writeParcelableArray() and convert the list to array.
i have three classes and want to store all the data of three classes... these classes are composed in main class ... so can any one tell that whether i have implement the Serilzable interface on class or all three classes which are composed....
please check this out http://developer.android.com/guide/topics/data/data-storage.html
it is according to your requirement. but i suggest to use the shared preference if the data is in key pair value.it store value in xml tag inside the application and also fast operation as compare to other thing.
for shared preference here
All objects in the object tree of a Serializable object must implement Serializable; otherwise you'll get a NotSerializableException.
Serialization is for entire object graph or none. For e.g you have the following scenario
Class A
{
class b = new class B();
class c = new class C();
class d = new class D();
}
Suppose if you want to serialize A then all of the composed objects of different class has to implement serializable including A if any of composed object fails to implement serializable then NoSerializableException will be thrown.