Android Serialization for saving the data... - android

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.

Related

How to pass an object to another activity? If the object already implements an interface (so it can't implement Parcelable)?

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

Implementing both Serializable and Parcelable interfaces from an object in Android - conflict

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.

How can I automatically create backreferences in RealmObjects when deserialized from JSON?

when I implemented my DB with OrmLite, I was able to let backreferences be created automatically. For example, imagine a class A which has a reference to a list of objects of class B and Class B has a reference back to that specific object of class A. With Gson/OrmLite, when parsing the following JSON it would create a obejct of class A with a ForeignCollection of Class B objects and for every class B object it would create a reference to the class A object (if I am not mistaking anything here). Take the following json (representing an object of Class A):
{
"id":5,
"objectsOfTypeB": [
{
"id":2,
"name":"pete"
},
{
"id":4,
"name":"mathew"
}]
}
Now I would like to do the same with Realm. Is it somehow possible to create a backreference for nested objects? Maybe hook into the object-creation process and set the reference manually?
Christian from Realm here.
The core database support the notion of backlinks, which is what you are looking for, but they havn't been exposed in the Java API yet. This means that until we can add support for them, you will manually have to create and maintain these relationships. That can only be done if you do the import manually as it is not possible to hook into the current JSON import.

Need for Parcelable in android

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.

How to preserve Object data without passing between activities within Android?

Actually,my application flow is like this Home->A->B->Info(form data)->D->Final page.From final page if I press on one button it again navigates back to A page and start the flow from onwards.If I comes to info page I should display the earliear data.Right now my approach is passing parcelable object within all acitivities from A->B->Info->D->Final.If suppose want to use Preferences, doesn't supports the parcelable object and don't want to put each string of object individually within preferences becaus I had more than 10 items within object.Is there any better approach without passing bundle between actvities.
BR,
Developer.
you can create Global class and declare Static variables and use them in anyware in the application.
Example:
public class global_variable {
public static String sample ;
}
where you want to use ;
global_variable.sample = "your value";
You could use any number of technologies to parse your data object into a string and reassemble again. Then you could store the string in preferences.
Take a look at gson to convert objects to json http://code.google.com/p/google-gson/
Or you could google xstream to convert to xml
If you create a class representing your 'object' with appropriate setters/getters and let that class implement Parceable and then pass that class between Activites as a Parceable in a Bundle, would that be bad?
If that would be bad (e.g. if the amount of object data is very big or they are somehow not Parceable in principle) and you only have one meaningful instance of a class at a time you can make that class a singleton or keep it within your Application object.

Categories

Resources