Save and retrieve a list by rotating the device in Android Java

1

I have a List<Sendero> that is populated from a database, I want to avoid recharging data from the database when the device rotates

I mean I want to save the list so I can get it back in onCreate using savedInstanceState

To save

The same Android-Studio generates the following code:

private List<Route> listData;

@Override
public void onSaveInstanceState(Bundle outState) {
    outState.putParcelableArrayList("list_data", (ArrayList<? extends Parcelable>) listData);
    super.onSaveInstanceState(outState);
}

The problem comes when it comes to recovering, a warning indicates.

if (savedInstanceState != null) {
   listData = (List<Route>) savedInstanceState.getSerializable("list_data");
}

Warning following:

Unchecked cast: 'java.io.Serializable'  to  'java.util.List<app.....models.Route>'
    
asked by Webserveis 15.10.2016 в 20:18
source

2 answers

1

I see that what you do when saving and retrieving the List is correct, but your object must implement Parcelable a>:

public class Route implements Parcelable {
    
answered by 16.10.2016 / 19:40
source
1

Include this in your if it should serve (Well, I would put the list as a class variable):

ArrayList<? extends Parcelable> listData;

if (savedInstanceState != null) {
  listData = ( ArrayList<? extends Parcelable>)   savedInstanceState.getSerializable("list_data");
}
    
answered by 15.10.2016 в 20:25