Java – If you reopen the application, SharedPreferences is not saved

If you reopen the application, SharedPreferences is not saved… here is a solution to the problem.

If you reopen the application, SharedPreferences is not saved

If I reopen my game, my sharedpreferences won’t save my previously saved data with SharedPreferences won’t load, and the settings for my current activity will return to normal or default again

Here is an image of my button in menu.class

enter image description here

Here is the following code for my menu.class

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.menu);
    SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE); 
    SharedPreferences.Editor editor = pref.edit();      
    editor.putInt("Lifes", 6);
    editor.putInt("Hints", 6);          
    editor.putInt("Level", 1);  
    editor.commit();

f1=(Button)findViewById(R.id.f1);

f2=(Button)findViewById(R.id.f2);
     f2lock=(ImageView)findViewById(R.id.f2lock);

f1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                 TODO Auto-generated method stub
                Intent i =new Intent(menu.this, levelone.class);
                startActivity(i);             
            }             
        }); 

f2.setOnClickListener(new View.OnClickListener() {

@Override
         public void onClick(View v){
            TODO Auto-generated method stub
              Intent i =new Intent(menu.this, leveltwo.class);
              startActivity(i);          
            }             
      });

f3=(Button)findViewById(R.id.f3);
    f3lock=(ImageView)findViewById(R.id.f3lock);

}
public void onResume() {
super.onResume();

SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE); 
   levelunlocked = pref.getInt("Level", 0); 

if(levelunlocked == 2)

{
        f2.setVisibility(View.VISIBLE);
        f2lock.setVisibility(View.GONE);
    }
    if(levelunlocked == 3)

{
        f3.setVisibility(View.VISIBLE);
        f3lock.setVisibility(View.GONE);
    }   

SharedPreferences.Editor editor = pref.edit();
       editor.putInt("Level", levelunlocked);
       editor.commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
     Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.splashscreen, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
     Handle action bar item clicks here. The action bar will
     automatically handle clicks on the Home/Up button, so long
     as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

I have this code in levelone.class to get the default value from menu.class

int gamelifes, gamehints, gamelevel, index=0; 

SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE);
gamelifes = pref.getInt("Lifes", 0);
gamehints = pref.getInt("Hints", 0);
gamelevel = pref.getInt("Level", 0);

the value from sharedpreferences is use to be a text by use code below

lifes1 =(TextView)findViewById(R.id.lifestext1);
lifes1.setTextColor(Color.RED);
lifes1.setText(String.valueOf(gamelifes));   

hints1 =(TextView)findViewById(R.id.hintstext1);
hints1.setTextColor(Color.GRAY);
hints1.setText(String.valueOf(gamehints));

and save sharing preferences with new data

String answer=edittextanswer1.getText().toString();              
            if(answer.equalsIgnoreCase(answer1[index]))
            {
                gamelevel++;                
                image.setVisibility(View.GONE); 
                finishbutton.setVisibility(View.VISIBLE);  
                SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE); 
                SharedPreferences.Editor editor = pref.edit();      
                editor.putInt("Lifes", gamelifes);
                editor.putInt("Hints", gamehints);          
                editor.putInt("Level", gamelevel);  
                editor.commit();
            else
            {    
            tryagain1.setVisibility(View.VISIBLE);
            gamelifes--;
            lifes1.setText(String.valueOf(gamelifes));
            }

Then if you hit the Done button, it will look like this

finishbutton.setOnClickListener(new View.OnClickListener() {

public void onClick(View v){
          finish();
      }
   }); 

So levelone.class completes and returns to the menu .class

enter image description here

The SharedPreferences code works fine, and every button in my menu.class works with the code and is visible!

But if I quit the app, it goes back to normal again

enter image description here

Is there a way for someone to solve my problem?

Solution

There is the following line in the onCreate method:

SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE); 
SharedPreferences.Editor editor = pref.edit();      
editor.putInt("Lifes", 6);
editor.putInt("Hints", 6);          
editor.putInt("Level", 1);  
editor.commit();

The onCreate method runs every time the actvity lifecycle needs it. Especially when opening an app. Therefore, each time you create an activity, you set the preference level to 1. That’s why you’re having trouble.

Related Problems and Solutions