1

I have a menu activity that acts as a level selector. When the user finishes a level (activity) and returns to the menu, I want it to set an image visible in the menu to show that said level has been completed. Also, I want this info to save from a session to another.

My aproach has been to try SharedPreferences. This is the code from the level activity that writes the save:

private int levelNumber

SharedPreferences save = getSharedPreferences("SaveGame", MODE_PRIVATE);
SharedPreferences.Editor editor = save.edit();
editor.putInt("levelComplete",levelNumber);
editor.commit();

GameActivity.this.finish();

This is the relevant code from the menu that reads the save and sets the image visible based on the levelNumber:

clear_stage = new ImageView[clear];

clear_stage[0] = (ImageView)findViewById(R.id.clear1);
clear_stage[1] = (ImageView)findViewById(R.id.clear2);
clear_stage[2] = (ImageView)findViewById(R.id.clear3);
clear_stage[3] = (ImageView)findViewById(R.id.clear4);
clear_stage[4] = (ImageView)findViewById(R.id.clear5);

SharedPreferences save = getApplicationContext().getSharedPreferences("SaveGame", MODE_PRIVATE); 
clear = save.getInt("levelComplete", 0);

clear_stage[clear].setVisibility(View.VISIBLE);

I have the visibility set to "gone" in the xml, by the way.

Now, my main problem is that the visible image changes every time that a level is completed, and the last image goes invisible again (gets overwritten). Is there a way to store the clear value each time a level is complete?

I tried with:

SharedPreferences saveall = getSharedPreferences("SaveGame", MODE_PRIVATE);
SharedPreferences.Editor editor = saveall.edit();
editor.putInt("clear_stage",clear);
editor.commit();

But it isn't working, and I don't really know what I should do next. Ask me anything if the post wasn't clear enough.

Trybadisch
  • 13
  • 1
  • 4

1 Answers1

0

I think your problem is that your'e loading as an int, not an array. I would look here for a better explanation. Is it possible to add an array or object to SharedPreferences on Android particularyly sherifs' answer

Community
  • 1
  • 1
MattMatt
  • 905
  • 6
  • 19