I'm doing Quiz in Android Studio. The game is divided into two categories. After passing the first category, the second button (opening the second category) changes the status from setEnable "false" to "true". How to use the SharedPreferenced method in my code that the changes related to the second button (.setEnable) will be saved after closing the application.

Last level of first category

public class win extends AppCompatActivity implements View.OnClickListener{
Button win1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_winflagi);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    win1 = (Button) findViewById(R.id.winflagi);
    win1.setOnClickListener(this);

}

@Override
public void onClick(View v) {

    if(v==win1)
    {
        Intent myIntent = new Intent(win.this, Activity2.class);
        myIntent.putExtra("isEnabled", "enabled");
        startActivity(myIntent);

    }

}
}
  • Class containing a buttons for two categories ...
  • button3 opened the first category
  • entrycity opens the second category

    public class Activity2 extends AppCompatActivity implements View.OnClickListener{
    Button button3;
    Button entrycity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    
    setContentView(R.layout.activity_2);
    
    button3 = (Button) findViewById(R.id.button3);
    button3.setOnClickListener(this);
    
    entrycity = (Button) findViewById(R.id.entrycity);
    entrycity.setOnClickListener(this);
    
    }
    
    @Override
    public void onClick(final View v) {
    final MediaPlayer mp = MediaPlayer.create(this, R.raw.menu);
    if (v == button3) {
    
        startActivity(new Intent(Activity2.this, flagi1.class));
        Bungee.zoom(this);
        mp.start();
    
    }
    
    if (v== entrycity){
    
        Intent intent=getIntent();
        String isEnabled = intent.getStringExtra("isEnabled");
        if(isEnabled==null||isEnabled.equals("disabled")){
            entrycity.setEnabled(false);
        }
        else{
            entrycity.setEnabled(true);
            startActivity(new Intent(this, cities1.class));
        }
        }
    
    }
    
    }
    

Set a boolean preference when you are enabling the Button.

else{
     entrycity.setEnabled(true);
     getSharedPreferences("MY_PREF", MODE_PRIVATE).edit().putBoolean("isEnabled",true).apply();
     startActivity(new Intent(this, cities1.class));
    }

and in your onCreate set the enabled status of the button based on the preference :

entrycity.setEnabled(getSharedPreferences("MY_PREF", MODE_PRIVATE).getBoolean("isEnabled",false));
  • I tried it and when i'm exit from app, changes not saving :\ – Mr.Cheese Sep 24 at 20:57
  • oops.. Made a typo. The name of the sharedPreference is MY_PREF, not userdetails – nupadhyaya Sep 24 at 20:59
  • This code still not working :( – Mr.Cheese Sep 25 at 12:12
  • If you expect it to be written in the next Activity, then use commit() instead of apply(). And suppress the lint warning. – EpicPandaForce Sep 25 at 12:51
  • Do not compare view with "==" use equals() or compare views id (v.getId() == btn.getId()) – abalta Sep 25 at 14:05

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.