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.
Related
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.
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 want to know when to use parcelable and when not to . I know that parcelable is way of parcel complex data type in android , but as per official document http://developer.android.com/guide/faq/framework.html#3 , nothing such is mention . So when is parcelable really needed ??
By implementing the Parcelable interface, you can make your class object capable of being stored in a Bundle and you can then easily pass it to another activity with the help of an Intent.
e.g.
Intent i = new Intent(....);
i.putParcelableExtra("name", object);
Then in the other activity, get it like this:
YourClass object = (YourClass) getIntent().getExtras().getParcelableExtra("name");
Notice the typecasting. That is necessary in order to use your class's methods.
To pass data to another activity, we are usually put bundle object on activity intent that will be called. Bundle can be filled with primitive data types like long, integer, and boolean. It can be filled with simple data type like String class to represent text. For example, an activity calls another activity at the same time sends simple data to it.
In destination activity, we check the bundle. If it is exist, we open the data of bundle from the origin activity.
Now, how if we want to pass the complex data type like our defined class object to another activity? For this need, we can use Parcelable in Android.
Parcelable is an interface for classes so a class that implements Parcelable can be written to and read from a Parcel. The data in Parcel form can be passed between two threads. Parcel itself is a class that have abilities to serialize and deserialize object of class.
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.