I'm trying to handle orientation changes in my activity. I have a ListView that gets populated by a custom adapter. The adapter accepts an ArrayList<Listing>
where Listing
is custom object I have created. I know the ListView works but on an orientation change it loses my data.
I might be missing something obvious but after following all the tutorials and SO posts I still can't get this to work. I'm not trying to pass this to a new intent. I'm trying to handle orientation changes.
Here's my custom object that implements Parcelable:
package xx.xx.xx.xxxxxxx;
import android.os.Parcel;
import android.os.Parcelable;
public class Listing implements Parcelable {
private String name;
private String rating;
private String cuisines;
public Listing(){
}
public Listing(String name, String rating, String cuisines) {
this.name = name;
this.rating = rating;
this.cuisines = cuisines;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRating() {
return rating;
}
public void setRating(String rating) {
this.rating = rating;
}
public String getCuisines() {
return cuisines;
}
public void setCuisines(String cuisines) {
this.cuisines = cuisines;
}
private Listing(Parcel in) {
// Recreate Object using from the Parcelled String array
String[] data = new String[3];
in.readStringArray(data);
this.name = data[0];
this.rating = data[1];
this.cuisines = data[2];
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// Parcel using a String array
dest.writeStringArray(new String[] {
this.name,
this.rating,
this.cuisines
});
}
public static final Parcelable.Creator<Listing> CREATOR = new Parcelable.Creator<Listing>() {
@Override
public Listing createFromParcel(Parcel in) {
return new Listing(in);
}
@Override
public Listing[] newArray(int size) {
return new Listing[size];
}
};
}
In my MainActivity I have the following to try and handle configuration changes:
@Override
public void onSaveInstanceState(Bundle savedInstanceState){
super.onSaveInstanceState(savedInstanceState);
// Save the result ArrayList<Listing>
savedInstanceState.putParcelableArrayList("result", result);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Try and restore the List results
result = savedInstanceState.getParcelableArrayList("result");
adapter.notifyDataSetChanged();
}
I don't get any obvious errors but when I debug and add a break point in my code I can see that savedInstanceState
has mParcelledData = null
. I take that to mean that I'm not using putParcelableArrayList
correctly.
What would help massively is if someone can tell me how to debug this in a better way so I can provide better information in the question or solve this myself.
Thanks
Update:
When I look in savedInstanceState
I see that mMap has java.lang.ClassNotFoundException
exceptions where I am hoping key value pairs are. Is this of interest to solving the question?