Passing Data Through Intent Using Serializable

How to pass an object from one activity to another on Android

One option could be letting your custom class implement the Serializable interface and then you can pass object instances in the intent extra using the putExtra(Serializable..) variant of the Intent#putExtra() method.

Actual Code:

In Your Custom Model/Object Class:

public class YourClass implements Serializable {

At other class where using the Custom Model/Class:

//To pass:
intent.putExtra("KEY_NAME", myObject);

myObject is of type "YourClass".
Then to retrieve from another activity, use getSerializableExtra
get the object using same Key name. And typecast to YourClass is needed:

// To retrieve object in second Activity
myObject = (YourClass) getIntent().getSerializableExtra("KEY_NAME");

Note: Make sure each nested class of your main custom class has implemented Serializable interface to avoid any serialization exceptions. For example:

class MainClass implements Serializable {

public MainClass() {}

public static class ChildClass implements Serializable {

public ChildClass() {}
}
}

Pass serializable object throught intent

From the official Serializable doc:

Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable.

Your base class doesn't implement Serializable, so its fields are not getting serialized.

Make MyItemBase (or even MyObject) Serializable and all of its descendants will also become Serializable (without explicitly implementing the interface).

Passing an object from the 2nd activity back to main activity using serializable in android

Much can't be said about your problem without proper log details.
But you can go through these points.
The problem with Serializable approach is that reflection is used and it is a slow process. This method create a lot of temporary objects and cause quite a bit of garbage collection. So, it might be due to this. Try running on a real device & see if it persists.
Alternatively, you can implement Parcelable to your class which is faster than Serializable.

Sending arraylist of objects using serializable

Use intent.putParcelableArrayListExtra("arrayListVenue",arrayListVenue); for putting arraylist to intent and use intent.getParcelableArrayExtra("arrayListVenue") for get arraylist back from intent in your SearchVenueActivity activity

Edit
Tutorial for using parcelable



Related Topics



Leave a reply



Submit