Pavle37
Pavle37

Reputation: 581

Put objects into bundle

Greetings,

I have a game, and i want to save the objects ( creatues ) that move on canvas to a bundle so that when someone pauses/leaves the app, the objects can stay where they were.

I have looked at the LunarLanding game where they save the coordinates of the space shuttle into bundles and read from them and i want to do same ( if there is no better way ) but i have objects of a custom type and i am not sure how to save them and read from the bundle.

I could do the save of all the parts of the object individually and put them back together, but i have a lot of objects and that would just be ugly code to do all that.

It would be just great if i could save an object to a bundle, but so far i had no luck searching the internet on how to do so.

I was also thinking about implementing Parcelable to my object, but i want to know if there is any other way.

Any suggestions?
Thanks!

Upvotes: 1

Views: 2964

Answers (2)

EpicPandaForce
EpicPandaForce

Reputation: 81568

Technically, the onSaveInstanceState() method is called mostly only on orientation change if I remember correctly. If you want to make persistent data, you should use the onPause() or onStop() callback, and serialize the game state.

The ways you can do that is either by storing the state in a SQLite database (seems overkill), or make it so that you can serialize the object that keeps track of the entities via implementing the Serializable interface, and save the object to a file.

Serialization:

@Override
public void onPause()
{
    super.onPause();
    FileOutputStream out = null;
    try
    {
        out = openFileOutput("GameModelBackup",Context.MODE_PRIVATE);

        try
        {
            ObjectOutputStream oos = new ObjectOutputStream(out);
            oos.writeObject(gm);
        }
        catch(IOException e)
        {
            Log.d(this.getClass().toString(), e.getMessage());
        }
    }
    catch(FileNotFoundException e)
    {
        Log.d(this.getClass().toString(), e.getMessage());
    }
    finally
    {
        try
        {
            if(out != null) out.close();
        }
        catch(IOException e)
        {
            Log.d(this.getClass().toString(), e.getMessage());
        }
    }
} 

Deserialization:

@Override
public void onResume()
{
    super.onResume();
    FileInputStream in = null;
    try
    {
        in = openFileInput("GameModelBackup");
        ObjectInputStream oos = new ObjectInputStream(in);
        try
        {
            gm = (GameModel)oos.readObject();
        }
        catch(ClassNotFoundException e)
        {
            gm = null;
        }
    }
    catch(IOException e)
    {
        Log.d(this.getClass().toString(), e.getMessage());
    }
    finally
    {
        try
        {
            if(in != null) in.close();
        }
        catch(IOException e) {}
    }
}

Upvotes: 1

Pozzo Apps
Pozzo Apps

Reputation: 1857

Basically you have 2 options

1 - Implement Serializable, really simple to implement, but has a bad drawback which is performance.

2 - Implement Parcelable, very fast, but you need to implement the parser method (writeToParcel()), which basically you have to serialize manually, but afterwards bundle will call it automatically for you and will produce a much more performatic serialization.

Upvotes: 2

Related Questions