0

I have a problem with orientation change andloss of data in my fragment. So basically I have a parcelable class and array of that class, but for some reason it gives me an error: enter image description here

Parcel class:

public final class WeatherData implements Parcelable {

public static final String CELSIUS_PLACEHOLDER = " °C";
public static final String PASCAL_PLACEHOLDER = " hPa";
public static final String HUMIDITY_PERCENT_PLACEHOLDER = " %";

public final String tempMin;
public final String tempMax;
public final String cityName;
public final String description;
public final String humidity;
public final String pressure;
public final String icon;
public final String date;

public WeatherData(final String tempMin, final String tempMax, final String cityName, final String description, final String humidity, final String pressure,
                   final String icon, final String date) {
    this.tempMin = tempMin;
    this.tempMax = tempMax;
    this.cityName = cityName;
    this.description = description;
    this.humidity = humidity;
    this.pressure = pressure;
    this.icon = icon;
    this.date = date;
}


@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(final Parcel dest, final int flags) {
    dest.writeString(this.tempMin);
    dest.writeString(this.tempMax);
    dest.writeString(this.cityName);
    dest.writeString(this.description);
    dest.writeString(this.humidity);
    dest.writeString(this.pressure);
    dest.writeString(this.icon);
}

public WeatherData(final Parcel in) {
    this.tempMin = in.readString();
    this.tempMax = in.readString();
    this.cityName = in.readString();
    this.description = in.readString();
    this.humidity = in.readString();
    this.pressure = in.readString();
    this.icon = in.readString();
    this.date = in.readString();
}

public static final Parcelable.Creator<WeatherData> CREATOR = new Parcelable.Creator<WeatherData>() {

    @Override
    public final WeatherData createFromParcel(Parcel source) {
        return new WeatherData(source);
    }

    @Override
    public final WeatherData[] newArray(int size) {
        return new WeatherData[size];
    }
};

In my fragment I put the list as :

final List<WeatherData> weatherDataList = new ArrayList<>();
hyrulelink16
  • 389
  • 1
  • 5
  • 17

2 Answers2

0

Instead of implementing Parcelable, extend it to WeatherData class

Sanjeet
  • 2,385
  • 1
  • 13
  • 22
0

I just put the android:configChanges="keyboardHidden|orientation|screenSize" in my manifest file for activity and it works without all the mumbo jumbo :)

hyrulelink16
  • 389
  • 1
  • 5
  • 17