I have a custom object class but that is implemented through an inteface, how can i incorporate parceable in it. I have followed and searched about parceable, but it is only for object class. eg : How can I make my custom objects Parcelable?
I want to pass my object list to another activity in android.
code :
public interface Projection {
interface Job {
@XBRead("./task")
List<Task> getTasks();
@XBRead("./id")
String getid();
@XBRead("./job_title")
String getjob_title();
@XBRead("./job_description")
String getjob_description();
@XBRead("./job_room")
String getjob_room();
@XBRead("./status")
String getstatus();
}
interface Task {
@XBRead("./task_id")
String gettask_id();
@XBRead("./task_title")
String gettask_title();
@XBRead("./task_description")
String gettask_description();
@XBRead("./task_status")
String gettask_status();
}
@XBRead("/root/job")
List<Job> getJobs();
}
Create Parcelable class without plugin in Android Studioimplements Parcelable in your class and then put cursor on "implements Parcelable" and hit Alt+Enter and select Add Parcelable implementation (see image). that's it.
If a developer wants to convert a Java object into Parcelable, then the best way to do so is by implementing the Parcelable interface and overriding the writeToParcel() methods in its own class. The first step is to override the writeToParcel() method and write all object members into parcel objects.
Serializable is also an interface and it's not a part of Android SDK. Parcelable is the Android implementation of Java serializable. Parcelable is relatively fast as compared to the java serialization. It's recommended to use Parcelable.
The Parcelable interface adds methods to all classes you want to be able to transfer between activities. These methods are how parcelable deconstructs the object in one activity and reconstructs it in another. For this example you'll look at how to implement parcelable in a simple class.
Your custom interfaces need to extend Parcelable
.
Classes that implement your custom interface need to also implement the Parcelable
interface, including the CREATOR
.
You can then add an object implementing your custom interface to an Intent
like this:
intent.putExtra("thing", thing);
or add an ArrayList
containing these objects like this:
ArrayList<Thing> things;
intent.putParcelableArrayListExtra("things", things);
On the receiving end, the Activity
can extract the objects from the Intent
like this:
Thing thing = intent.getParcelableExtra("thing");
or
ArrayList<Thing> things = intent.getParcelableArrayListExtra("things");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With