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
How to Get Stock Quotes Using Google Finance API
Download a File With Android, and Showing the Progress in a Progressdialog
Inner Class Can Access But Not Update Values - Asynctask
Concurrent Modification Exception: Adding to an Arraylist
Growing Resident Memory Usage (Rss) of Java Process
Should I Avoid the Use of Set(Preferred|Maximum|Minimum)Size Methods in Java Swing
What Does the 'Static' Keyword Do in a Class
What Is Reflection and Why Is It Useful
Java Url Encoding of Query String Parameters
What Causes Error "No Enclosing Instance of Type Foo Is Accessible" and How to Fix It
Why Is It Considered a Bad Practice to Omit Curly Braces
Launching Google Maps Directions Via an Intent on Android
Passing Data Through Intent Using Serializable
Downloading Java Jdk on Linux Via Wget Is Shown License Page Instead
What's the Simplest Way to Print a Java Array