Implementing Parcelable - android

I have implemented parcelable in many of my classes that only have String member variables. I am not sure how to do it for a class that has an ArrayList. What do I do in the constructor that takes in the source (Parcel)?
public class Schedule {
private ArrayList<Event> allEvents;
public Schedule(Parcel source) {
}
public Schedule(){
allEvents = new ArrayList<Event>();
}
//Getter
public ArrayList<Event> getAllEvents(){
return allEvents;
}
public void addEvent(Event newEvent){
allEvents.add(newEvent);
}
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeList(allEvents);
}
public static final Parcelable.Creator<Schedule> CREATOR =
new Parcelable.Creator<Schedule>() {
#Override
public Schedule createFromParcel(Parcel source) {
return new Schedule(source);
}
#Override
public Schedule[] newArray(int size) {
return new Schedule[size];
}
};
}

Related

Parcable of generic array

I generate parcelable implementation with plugin and that is the code i got.
I have a an error compilation on Metrics(Parcel in).
It probably because the generic array. How can i fix it?
The generic T can can contain String, Number or boolean only.
public class Metrics<T extends Parcelable> implements Parcelable {
private T[] yData;
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeTypedArray(this.yData, flags);
}
public Metrics() {
}
protected Metrics(Parcel in) {
this.yData = in.createTypedArray(T.CREATOR);
}
public static final Parcelable.Creator<Metrics> CREATOR = new Parcelable.Creator<Metrics>() {
#Override
public Metrics createFromParcel(Parcel source) {
return new Metrics(source);
}
#Override
public Metrics[] newArray(int size) {
return new Metrics[size];
}
};
}
You can use a List instead of an array :
public class Metrics<T extends Parcelable> implements Parcelable {
private List<T> yData;
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
if (yData == null || yData.size() == 0)
dest.writeInt(0);
else {
dest.writeInt(yData.size());
final Class<?> objectsType = yData.get(0).getClass();
dest.writeSerializable(objectsType);
dest.writeList(yData);
}
}
public Metrics() {
}
protected Metrics(Parcel in) {
int size = in.readInt();
if (size == 0) {
yData = null;
} else {
Class<?> type = (Class<?>) in.readSerializable();
yData = new ArrayList<>(size);
in.readList(yData, type.getClassLoader());
}
}
public static final Parcelable.Creator<Metrics> CREATOR = new Parcelable.Creator<Metrics>() {
#Override
public Metrics createFromParcel(Parcel source) {
return new Metrics(source);
}
#Override
public Metrics[] newArray(int size) {
return new Metrics[size];
}
};
}

Storing ArrayList in onSavedInstanceState

I'm running an asynctask in a Fragment class and populating a ListView with the result. However I only run this asynctask the first time this fragment is instantiated.
How do I store the the ArrayList of objects created in the asynctask in onSavedInstance so I can retrieve them later?
I tried using putParcelableArrayList but I still get a NullPointerException when I try to retrieve it.
Thanks for your help
Stops:
public class Stops implements Parcelable{
private String station_name;
private ArrayList<Stops> stopTitles;
public Stops()
{
}
public Stops(Parcel source)
{
station_name = source.readString();
source.readTypedList(stopTitles, Stops.CREATOR);
}
public String getStation_name() {
return station_name;
}
public void setStation_name(String station_name) {
this.station_name = station_name;
}
public void setStopTitles(ArrayList<Stops> stopTitles) {
this.stopTitles = stopTitles;
}
public ArrayList<Stops> getStopTitles() {
return stopTitles;
}
#Override
public String toString() {
return station_name;
}
#Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
#Override
public void writeToParcel(Parcel dest, int arg1) {
// TODO Auto-generated method stub
dest.writeString(station_name);
dest.writeList(stopTitles);
}
public static final Parcelable.Creator<Stops> CREATOR = new Parcelable.Creator<Stops>() {
#Override
public Stops createFromParcel(Parcel in) {
return new Stops(in);
}
#Override
public Stops[] newArray(int size) {
return new Stops[size];
}
};
}
Have you made a Parcelable implementation of the Object you are holding in the ArrayList? If the object is not parcelable you cannot pass the list to onSavedInstance example code:
public class ParcelableObject implements Parcelable {
String field;
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(field);
}
public static final Parcelable.Creator<ParcelableObject> CREATOR =
new Parcelable.Creator<ParcelableObject>() {
public ParcelableObject createFromParcel(Parcel in) {
ParcelableObject obj = new ParcelableObject();
obj.readFromParcel(in)
return obj;
}
public ParcelableObject[] newArray(int size) {
return new ParcelableObject[size];
}
};
private void readFromParcel(Parcel in) {
field = in.readString()
}
}

How to extend android class which implements Parcelable interface?

First of all i have check this answer.
What i am trying to do is extending Location class calling it LocationPlus which has some
member variables. functionality i am trying to achieve is pass the object of LocationPlus class from one activity to another.
Here is my CREATOR
public static final Parcelable.Creator<LocationPlus> CREATOR = new Parcelable.Creator<LocationPlus>() {
#Override
public LocationPlus createFromParcel(Parcel source) {
return new LocationPlus(source);
}
#Override
public LocationPlus[] newArray(int size) {
return new LocationPlus[size];
}
};
problem i am facing is this error
Implicit super constructor Location() is undefined. Must explicitly invoke another constructor
when trying to write constructor
public LocationPlus(Parcel in) {
Someone in comment ask me to post LocationPlus class so here it is
public class LocationPlus extends Location{
private int mBattery = -1;
public LocationPlus(String locationName) {
super(locationName);
}
public LocationPlus(Location location) {
super(location);
}
public int getmBattery() {
return mBattery;
}
public void setmBattery(int mBattery) {
this.mBattery = mBattery;
}
#Override
public int describeContents() {
return 0;
}
public static final Parcelable.Creator<LocationPlus> CREATOR = new Parcelable.Creator<LocationPlus>() {
#Override
public LocationPlus createFromParcel(Parcel source) {
return new LocationPlus(source);
}
#Override
public LocationPlus[] newArray(int size) {
return new LocationPlus[size];
}
};
#Override
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeInt(mBattery);
}
public LocationPlus(Parcel in) {
mBattery =in.readInt();
}
}
Parcelable, the Speed King
According to google engineers, this code will run significantly faster. One of the reasons for this is that we are being explicit about the serialization process instead of using reflection to infer it. It also stands to reason that the code has been heavily optimized for this purpose.
public abstract class BaseClass implements Parcelable {
public String FullName;
public boolean IsValidUser;
public String UserName;
public BaseClass () {
}
protected BaseClass(Parcel in) {
FullName = in.readString();
IsValidUser = in.readByte() != 0;
UserName = in.readString();
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(FullName);
dest.writeByte((byte) (IsValidUser ? 1 : 0));
dest.writeString(UserName);
}
}
Child class will be as follows with usage of list adding into parcelable object:
public class DerivedClass extends BaseClass {
public boolean IsSuccess;
public String Message;
public List<AnotherClass> AnotherClassObj;
public DerivedClass () {
super();
}
protected DerivedClass(Parcel in) {
super(in);
AnotherClassObj = new ArrayList<AnotherClass>();
IsSuccess = in.readByte() != 0;
Message = in.readString();
AnotherClassObj = in.readArrayList(AnotherClass.class.getClassLoader());
}
public static final Creator<DerivedClass> CREATOR = new Creator<DerivedClass>() {
#Override
public DerivedClass createFromParcel(Parcel in) {
return new DerivedClass(in);
}
#Override
public DerivedClass[] newArray(int size) {
return new DerivedClass[size];
}
};
#Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeByte((byte) (IsSuccess ? 1 : 0));
dest.writeString(Message);
dest.writeList(AnotherClassObj);
}
public int describeContents() {
return 0;
}
}
Another child class :
public class AnotherClass extends BaseClass {
public AnotherClass() {
super();
}
protected AnotherClass(Parcel in) {
super(in);
}
public int describeContents() {
return 0;
}
public static final Creator<AnotherClass> CREATOR = new Creator<AnotherClass>() {
#Override
public AnotherClass createFromParcel(Parcel in) {
return new AnotherClass(in);
}
#Override
public AnotherClass[] newArray(int size) {
return new AnotherClass[size];
}
};
#Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
}
}
In Activity:
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.putExtra("UserObject", parcelableObject);
startActivity(intent);
finish();
In receiving activity:
Bundle extras = getIntent().getExtras();
if (extras != null) {
userObject = extras.getParcelable("UserObject");
}
Hi I've do research a lot about this, but I couldn't find useful anything. I try solution below and it worked for me.
Let say your super class has only int variable named "mData".
public class Location implements Parcelable {
protected int mData;
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel out, int flags) {
out.writeInt(mData);
}
public static final Parcelable.Creator<Location> CREATOR
= new Parcelable.Creator<Location>() {
public Location createFromParcel(Parcel in) {
return new Location(in);
}
public Location[] newArray(int size) {
return new Location[size];
}
};
private Location(Parcel in) {
mData = in.readInt();
}
}
Then, your extended class has only int variable named "mBattery".
public class LocationPlus extends Location {
protected int mBattery;
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel out, int flags) {
out.writeInt(mBattery);
}
public static final Parcelable.Creator<LocationPlus> CREATOR
= new Parcelable.Creator<LocationPlus>() {
public LocationPlus createFromParcel(Parcel in) {
return new LocationPlus(in);
}
public LocationPlus[] newArray(int size) {
return new LocationPlus[size];
}
};
private LocationPlus(Parcel in) {
mBattery = in.readInt();
}
}
So far, LocationPlus works fine. But we don't set variable of super class. Firstly, I set super class' variables on extended class with super(..) method. But it didn't work.
private LocationPlus(Parcel in) {
super(in);
mBattery = in.readInt();
}
Instead of code above, you should set all super class' variables explicitly. Super class' variables should be protected. Final constructor should be like this:
private LocationPlus(Parcel in) {
mData = in.readIn();
mBattery = in.readInt();
}
and writeToParcel method should be like this:
public void writeToParcel(Parcel out, int flags) {
out.writeIn(mData);
out.writeInt(mBattery);
}
Try this solution:
public static final Parcelable.Creator<LocationPlus> CREATOR =
new Parcelable.Creator<LocationPlus>() {
#Override
public LocationPlus createFromParcel(Parcel in) {
Location l = Location.CREATOR.createFromParcel(in);
LocationPlus lp = new LocationPlus(l);
lp.mBattery= in.readInt();
return lp;
}
#Override
public LocationPlus[] newArray(int size) {
return new LocationPlus[size];
}
};
#Override
public void writeToParcel(Parcel parcel, int flags) {
super.writeToParcel(parcel, flags);
parcel.writeInt(mBattery);
}
According to the Android docs, there isn't a Location() constructor for the Location class. When initializing your LocationPlus class, you need to call either super(String provider) or super(Location l).
Edit: Corrected syntax
(See Location Android Doc)

How to implement Parcelable for a class containing List<List<String>>?

I have a working Parcelable implementaion for all the fields in my Parcelable Class apart from List<List<String>>
class Employee implements Parcelable {
List<List<String>> details;
//.......
protected Employee(Parcel in) {
details = new ArrayList<List<String>>();
// i know this is wrong just posting to clarify
in.readList(details, List.class.getClassLoader());
//......
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeList(details);
//.....
}
public int describeContents() {
return 0;
}
public static final Parcelable.Creator<Employee> CREATOR =
new Parcelable.Creator<Employee>() {
public Employee createFromParcel(Parcel in) {
return new Employee(in);
}
public Employee[] newArray(int size) {
return new Employee[size];
}
};
}
Exception:
05-10 19:07:44.072: E/AndroidRuntime(10661): Caused by: java.lang.RuntimeException: Parcel android.os.Parcel#42a509e8: Unmarshalling unknown type code 3604535 at offset 268
Extending ArrayList and implementing Parcelable on it worked for me.
public class ParcelableArrayList extends ArrayList<String> implements
Parcelable {
private static final long serialVersionUID = -8516873361351845306L;
public ParcelableArrayList(){
super();
}
protected ParcelableArrayList(Parcel in) {
in.readList(this, String.class.getClassLoader());
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeList(this);
}
public static final Parcelable.Creator<ParcelableArrayList> CREATOR =
new Parcelable.Creator<ParcelableArrayList>() {
public ParcelableArrayList createFromParcel(Parcel in) {
return new ParcelableArrayList(in);
}
public ParcelableArrayList[] newArray(int size) {
return new ParcelableArrayList[size];
}
};
}
and Employee class
class Employee implements Parcelable {
List<ParcelableArrayList> details;
//.......
protected Employee(Parcel in) {
details = new ArrayList<ParcelableArrayList>();
in.readTypedList(details,ParcelableArrayList.CREATOR);
//......
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeList(details);
//.....
}
public int describeContents() {
return 0;
}
public static final Parcelable.Creator<Employee> CREATOR =
new Parcelable.Creator<Employee>() {
public Employee createFromParcel(Parcel in) {
return new Employee(in);
}
public Employee[] newArray(int size) {
return new Employee[size];
}
};
}
I would create a class that extends List, implement Parcelable on that class. You can otherwise treat it as a normal list, but allow it to be parcelable.
Create class DetailsEntry implements Parcelable which contains List<String> and use List<DetailsEntry> details in Employee.

How to parcel an Object with an ArrayList with other Object which have a Reverence to the first Object

It's a little bit complex.
I've Object 'A' which have an ArrayList of Object 'B'
Object 'B' have a Reference to Object 'A'.
When I'm starting to parcel, then follows a StackoverflowError.
Is there a Workarround to fix it.
Here my classes:
Freizeit.java
public class Freizeit implements Parcelable{
private List<Tag> tage;
public static final String FREIZEIT = "freizeit";
private Freizeit (Parcel in) {
readFromParcel(in);
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeTypedList(this.tage);
}
public void readFromParcel(Parcel in) {
in.readTypedList(this.tage, Tag.CREATOR);
}
public static final Parcelable.Creator<Freizeit> CREATOR = new Parcelable.Creator<Freizeit>() {
#Override
public Freizeit createFromParcel(Parcel source) {
return new Freizeit (source);
}
#Override
public Freizeit[] newArray(int size) {
return new Freizeit[size];
}
};
}
Tag.java
public class Tag implements Parcelable {
private Freizeit freizeit;
public Tag (Freizeit freizeit) {
this.freizeit = freizeit;
}
private Tag (Parcel in) {
readFromParcel(in);
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelable(this.freizeit, flags);
}
public void readFromParcel(Parcel in) {
this.freizeit = in.readParcelable(Freizeit.class.getClassLoader());
}
public static final Parcelable.Creator<Tag> CREATOR = new Parcelable.Creator<Tag>() {
#Override
public Tag createFromParcel(Parcel source) {
return new Tag (source);
}
#Override
public Tag[] newArray(int size) {
return new Tag[size];
}
};
Sorry for my bad english...
Has anyone an idea? Thx.

Categories

Resources