Passing a List through as a ParcelableArrayList

前端 未结 1 1442
灰色年华
灰色年华 2021-01-26 05:27

So hey guys, Im having some trouble with some code. Im implementing parcelables. Basically I have an List of items initiated globally as

private List

        
相关标签:
1条回答
  • 2021-01-26 06:03

    Sadly Android doesn't "program to the interface". If the List is not an ArrayList then you'll have no choice but to create a new ArrayList. This isn't a big deal and can be done programmatically.

    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        ArrayList<Movie> toSave = mMovieList instanceof ArrayList ?
            (ArrayList<Movie>)mMovieList : new ArrayList<Movie>(mMovieList);
        outState.putParcelableArrayList(MOVIE_KEY, toSave);
    }
    
    0 讨论(0)
提交回复
热议问题