Is Using Serializable in Android Bad

Is using Serializable in Android bad?

For in-memory use, Parcelable is far, far better than Serializable. I strongly recommend not using Serializable.

You can't use Parcelable for data that will be stored on disk (because it doesn't have good guarantees about data consistency when things change), however Serializable is slow enough that I would strongly urge not using it there either. You are better off writing the data yourself.

Also, one of the performance issues with Serializable is that it ends to spin through lots of temporary objects, causing lots of GC activity in your app. It's pretty heinous. :}

Why we need to serializable object for passing one activity to another activity in Android

In ordinary java programs passing parameters(Object type), is kind of create a new handler to the object and giving to another method (In regular words passing the reference by value).

But when it comes in android, passing object references from activity to activity, where their states have to be persisted, is a serious headache.

One way you can do is create a static object in the first activity and access from the second, though this seems to be a easiest way, there is no guarantee that the system maintains the activity in the memory. Therefore the second activity may loose the object reference.

Other way, and the mostly recommended way is serializing(Kind of flatten the object) the object and pass with the intent as extra. In android there are two ways to serialize.

  1. Implement the java's serializable interface
  2. Implement the android's parcelable interface

However, on the android, there is a serious performance hit that comes with using serializable, the solution is using parcelable.

You can find a pretty good tutorial and explanation on android parcelable implementation here.

Serialization, is it bad to have many methods in a serialized object

Only the state of the object is serialized - the fields, in other words. The methods themselves are not serialized. However, the generated version number is also effectively part of the state, and that depends on what methods are present. You can change this behaviour with a serialVersionUid field, admittedly - but it's still tricky.

Personally I'd strongly recommend against using Java binary serialization - it ends up being really quite tricky to manage backward and forward compatilility. There are plenty of other serialization frameworks available, which usually require a bit more work to get started with than Java serialization, but end up being more portable and maintainable. Personally I'm a fan of Protocol Buffers but I'm biased :)

Android, Serializable/Parcelable problem in client-server app

We had the same issue here at work and we switched to JSON. Maybe you should consider doing the same?!

Edit: Android does know the Serializable interface. How could I forgot...

SQLite or Serialization

For me the only right solution would be to save the data inside a database especially if there are any dependencies. For beginners it might be hard at the beginning to get into database creation. but after you have created a database in the right form you just have to insert the data and you won't have any problems in the future if you want to change something or expand your app. With simple serialisation the logic has to be solved inside the app and might cause more problems especially if you have any dependencies.

If you need a good tutorial for saving data you should look at this tutorial
http://thenewboston.org/watch.php?cat=6&number=111

For other different solutions for saving data there are also some tutorials on the website, Nr. 108 - 110 of Android programming



Related Topics



Leave a reply



Submit