0

In my app (which is a game), I have an 'Enemy' class, for example like so:

public class Enemy extends Sprite implements Serializable {

    public Enemy(EnemyType type){
        super();
    }

}

I have then declared an ArrayList like so:

ArrayList<Enemy> enemyList = new ArrayList<Enemy>();

To which I can add enemies:

enemyList.add(bird);
enemyList.add(bee);

When saving to the Bundle I simply put:

bundle.putSerializable("Enemies", enemyList);

And when restoring from the Bundle, I have this:

enemyList = (ArrayList<Enemy>) savedState.getSerializable("Enemies");

Now, it does seem to restore the arraylist (I can check it's size and it is always correct - ie, the same size on restoring from the bundle, as it was when saving to the bundle.

I have also logged for example, the first index of the ArrayList and sure enough it lists the enemy instance as being there.

However, if I try to manipulate the ArrayList at any time post-restoration, I get an exception telling me that I'm trying to perform [whatever action] on a Null object (enemyList).

If I simply populate the list myself, so have something like:

enemyList = (ArrayList<Enemy>) savedState.getSerializable("Enemies");
enemyList.add(bird);
enemyList.add(bee);

Then everything works as expected.

I'm assuming this has something to do with the fact that the super class of Enemy isn't serialised? However, if I serialise this, I get a 'notSerializableException' error.

Please note, I'm not really too worried about saving/restoring the actual Enemy objects to the Bundle, I can handle this manually. Rather I just want the list to be in the same state as it was. And I thought that what was stored in the ArrayList were just references to the objects in question, therefore I can't work out why this is happening?

Any ideas what I'm doing wrong or is there a better method to achieve that which I'm trying to achieve?

Zippy
  • 3,826
  • 5
  • 43
  • 96
  • Rather I just want the list to be in the same state as it was, you mean your getting the correct value but UI not updated? – Lochana Ragupathy Mar 13 '15 at 14:45
  • why save list to `Bundle`? – Xcihnegn Mar 13 '15 at 14:46
  • @Xcihnegn, because I need it to be in the same state as it was before. This is to preserve the game state if the system kills the app (for example, user gets phone call during game, comes back to it an hour later after the OS has killed the app - I need everything to be preserved so it can pick up where it left off - saving the actual object's current state isn't an issue but I've no idea how to have the list left intact). – Zippy Mar 13 '15 at 14:52
  • @LochanaRagupathy - I'm really not sure, I guess that's what could be happening. All I know is that immediately after it's been restored, I can access it, but as soon as I switch back to the game proper, it see's it as null..... – Zippy Mar 13 '15 at 14:54
  • Save data when configure change, there is better way that is save data to anothe fragment that retain state – Xcihnegn Mar 13 '15 at 15:01
  • @Xcihnegn, Any properties that I need to be saved / restored are working OK using Bundle, it's just this ArrayList that I have a problem with :-) – Zippy Mar 13 '15 at 15:07

1 Answers1

0

The recommended way of doing this in Android is to make the objects you want to persist to be Parcelable. This is a type of serialization specific to Android. Have a look at the official documentation here

Rares Barbantan
  • 761
  • 4
  • 9
  • Thanks @RaresBarbantan but the problem isn't how to save an object's state, I can already do that (Actually, I've used Parcelable in the past, but currently do this manually by saving just the properties I want to rather than making the whole object Parcelable). The problem I'm currently facing is how to send and retrieve an ArrayList (full of custom objects) to/from the Bundle. I just need the ArrayList to be in the same state and to be able to access the objects within it (the objects are all recreated when the app is relaunched anyway so I don't need to save them anyway). – Zippy Mar 13 '15 at 15:03
  • That's exactly what the purpose of Parcelable is. Even the Bundle class is defined as "A mapping from String values to various Parcelable types" – Rares Barbantan Mar 13 '15 at 15:10
  • are you saying this isn't achievable using 'serializable'? (I've read a few posts that recommend using this). I'm currently not using Parcelable and would rather keep it like that if possible. – Zippy Mar 13 '15 at 15:18
  • It should be possible, just that it is about 10x slower. But for serialisation to work, all the fields also need to be serialisable. This means the Sprite class needs to be as well, which I doubt, assuming it has some kind of references to bitmaps for example (which are not serialisable). Unfortunately this also holds for Parcels – Rares Barbantan Mar 13 '15 at 15:29