Saving/Restoring custom ArrayList on Android scree

2019-06-14 05:09发布

问题:

I have a custom ArrayList of MovieItems that I want to save and restore on screen rotation. I checked out this post - How to save custom ArrayList on Android screen rotate? - and implemented and adapted some of the codes but the saving and restoring don't seem to be working for me.

1) Saving the ArrayList:

public List<MovieItem> mItems = new ArrayList<>();

@Override
public void onSaveInstanceState(Bundle outState) {

    outState.putParcelableArrayList(MOVIE_KEY, (ArrayList<? extends Parcelable>) mItems);
    super.onSaveInstanceState(outState);
}

2) Restoring the ArrayList:

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        if (savedInstanceState != null)
        {

            mItems = savedInstanceState.getParcelableArrayList(MOVIE_KEY);
        }
        setRetainInstance(true);
......

    }

3) Making the MovieItem class Parcelable:

public class MovieItem implements Parcelable{



    private static final long serialVersionUID = 1L;

    private String mCaption;
    private String mId;
    private String mUrl;
    private String plot;
    private Double rating;
    private String release_date;
    private boolean favourite;

    public MovieItem() {
    }

    protected MovieItem(Parcel in) {
        mCaption = in.readString();
        mId = in.readString();
        mUrl = in.readString();
        plot = in.readString();
        release_date = in.readString();
        favourite = in.readByte() != 0;
        mTitle = in.readString();
    }

    public static final Creator<MovieItem> CREATOR = new Creator<MovieItem>() {
        @Override
        public MovieItem createFromParcel(Parcel in) {
            return new MovieItem(in);
        }

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

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

    @Override
    public void writeToParcel(Parcel out, int flags) {

        out.writeString(mCaption);
        out.writeString(mId);
        out.writeString(mUrl);
        out.writeString(plot);
        out.writeDouble(rating);
        out.writeString(release_date);

    }

回答1:

add this line in your Manifest in relevant activity tag , and activity will handle rotation itself

android:configChanges="screenSize|orientation|screenLayout" 

test it maybe help you



回答2:

Because you have setRetainInstance(true), the Fragment is not destroyed/recreated on screen rotation. The Fragment will call onSaveInstanceState() after a screen rotation, but the onCreate() portion will not occur.



回答3:

Use singletone object for storing your list. public enum MySingleton { INSTANCE; private MySingleton() { System.out.println("Here"); } private List mItems = new ArrayList<>(); public List getItems(){ return mItems; } }

and for access to your array from anywhere MySingleton.INSTANCE.getItems()



回答4:

You can try implementing the onRestoreInstanceState() method as well. Its called after the onStart() method. You can read up about it onRestoreInstanceState