Save serializable object to persistent storage (android)

I need to store a complex custom object in persistent storage to keep the user's game progress in my application. Object and every object inside this object implementsSerializable

I am calling using load and save methods to pass the object between activities. I also call the method Save

onStop()

to save the object when the app is closed / destroyed. It works great.

 public static Game Load(Context context){
    try{
        FileInputStream fis = context.openFileInput("player1.data");
        ObjectInputStream is = new ObjectInputStream(fis);
        Game game = (Game) is.readObject();
        is.close();
        fis.close();
        return game;
    }catch (Exception e){
        Log.e("#Load", "creating new game - \n" + e.toString());
        Game newGame = new Game();
        return newGame;
    }
}


public static void Save(Context context,Game game){
    try{
        FileOutputStream fos = context.getApplicationContext().openFileOutput("player1.data", Context.MODE_PRIVATE);
        ObjectOutputStream os = new ObjectOutputStream(fos);
        os.writeObject(game);
        os.close();
        fos.close();
    }catch (Exception e){
        Log.e("#Save", "Failed to save - \n" + e.toString());
    }
}

      

However, if the application is forcibly closed, or closed and destroyed, the object Game

gets confused. On restart, the app will load Game

which was saved onStop()

, but when the object Game

changes / interacts with the user through the app, my app force is closed by the "stopped unexpectedly" message on the phone screen. From looking at the log, I know that when the object is Game

loaded on startup after a force close / destroy, the method Load

returns Game

, not newGame

, so somehow the save / load process between Force Closes something up.

I see the following in the log:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference

      

So, I am convinced that something strange is happening with the use Serializable

, or I am writing it wrong to persistent storage.

My questions are: Do you see something fundamentally wrong with my code? (keep in mind that this code does work, it just doesn't work under the Force Close circumstance)

I was told to use gson/JSON

(?) For my purposes; will this method have better results than Serializable

and how can I use it?

Update . It looks like when the Force stops happening, no methods are called, including onDestroy()

and onStop()

. If so, why isn't my application returning newGame

? Looking at the log while Force Closing, a message that said something about "onSavedInstance not called", what is it?

I have a feeling that I should completely avoid Serializable

...

onCreate snippet

Game game;
 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    game =  LoadGame.Load(this); //LoadGame is the class which contains Save,Load methods

@Override
protected void onStop() {
    LoadGame.Save(this,game);
    super.onStop();
}

      

+3


source to share


1 answer


Tips, Serialization is awesome, I use it all the time, the point is that you are actually saving the Thread that actually ran into the problem, are you expecting a magic call onStop()

? therefore either

  • You regularly call your save methods on another thread, so it keeps saving no matter what happens. - after all his play -

  • Perhaps you add try / catch to your code and catch certain exceptions and manually call the save method on another thread, and with that you need sick encapsulation. This may not serve you well, as bugs such as noSuchMethod

    etc cannot be caught so force closure can occur



anyway your preference.

+1


source







All Articles