-1

I am wondering how to save a feature across all pages when flipping between them in Android Studio.

I am trying to make a rigged dice rolling app that I want a setting to stick with the app until I turn it off.

Current code for the page where I want to have the setting save:

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


public void showToast(View view) {
    Toast toast = Toast.makeText(this, "You've switched a switch", Toast.LENGTH_SHORT);
    toast.show();
}


public void changeHomeScreen(View view) {
    startActivity(new Intent(getApplicationContext(), HomeScreen.class));
}
public void trollMode (View view){
    Switch s = findViewById(R.id.switch3);
    ImageView i = findViewById(R.id.imageView2);
    Button a = findViewById(R.id.button4);
    Button b = findViewById(R.id.button7);
    Button c = findViewById(R.id.button8);
    Button d = findViewById(R.id.button9);
    Button e = findViewById(R.id.button10);
    Button f = findViewById(R.id.button11);

    a.setVisibility(View.INVISIBLE);
    b.setVisibility(View.INVISIBLE);
    c.setVisibility(View.INVISIBLE);
    d.setVisibility(View.INVISIBLE);
    e.setVisibility(View.INVISIBLE);
    f.setVisibility(View.INVISIBLE);
    if (s.isChecked()){
        i.setImageResource(R.drawable.trollface);
        Toast toast = Toast.makeText(this, "You have ENABLED Troll Mode!", Toast.LENGTH_SHORT);
        toast.show();
        MediaPlayer myAudio = MediaPlayer.create(MainActivity.this, R.raw.evillaugh);
        myAudio.start();
    } else {
        i.setImageResource(R.drawable.dices);
        Toast toast = Toast.makeText(this, "You have DISABLED Troll Mode", Toast.LENGTH_SHORT);
        toast.show();
    }
 }
    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}


public void showToast(View view) {
    Toast toast = Toast.makeText(this, "You've switched a switch", Toast.LENGTH_SHORT);
    toast.show();
}


public void changeHomeScreen(View view) {
    startActivity(new Intent(getApplicationContext(), HomeScreen.class));
}
public void trollMode (View view){
    Switch s = findViewById(R.id.switch3);
    ImageView i = findViewById(R.id.imageView2);
    Button a = findViewById(R.id.button4);
    Button b = findViewById(R.id.button7);
    Button c = findViewById(R.id.button8);
    Button d = findViewById(R.id.button9);
    Button e = findViewById(R.id.button10);
    Button f = findViewById(R.id.button11);

    a.setVisibility(View.INVISIBLE);
    b.setVisibility(View.INVISIBLE);
    c.setVisibility(View.INVISIBLE);
    d.setVisibility(View.INVISIBLE);
    e.setVisibility(View.INVISIBLE);
    f.setVisibility(View.INVISIBLE);
    if (s.isChecked()){
        i.setImageResource(R.drawable.trollface);
        Toast toast = Toast.makeText(this, "You have ENABLED Troll Mode!", Toast.LENGTH_SHORT);
        toast.show();
        MediaPlayer myAudio = MediaPlayer.create(MainActivity.this, R.raw.evillaugh);
        myAudio.start();
    } else {
        i.setImageResource(R.drawable.dices);
        Toast toast = Toast.makeText(this, "You have DISABLED Troll Mode", Toast.LENGTH_SHORT);
        toast.show();
   }
 }

}

This is for the switch called "Troll Mode" which my method is called. I want, when it is switched on, to make buttons appear, and then when you click those buttons, I want it to reroute you to my home page. When you roll the dice on the home page while troll mode is on, then it will only show the number that you picked and nothing else until you turn troll mode off.

If anybody needs more information about the problem, I will be happy to give you what you need to the best of my ability.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Sam Biner
  • 1
  • 3

1 Answers1

0

I am trying to make a rigged dice rolling app that I want a setting to stick with the app until I turn it off.

Store the setting using SharedPreferences.

See the training documentation for a guide on how to use SharedPreferences.

And if you want to expose the setting to the user, use a PreferenceFragment.

dominicoder
  • 9,338
  • 1
  • 26
  • 32
  • What is the exact code that I should use for the SharedPreferences. It goes in way too much depth, and I only have a couple days to figure it out. – Sam Biner Oct 01 '18 at 21:03
  • I added a link to the training docs that show you how to use the code. If that's not enough, you should be able to Google for SharedPreferences to find plenty of examples. – dominicoder Oct 03 '18 at 00:31
  • I am using this code and it is totally messing up `TextView t = findViewById(R.id.textView2); ImageView v = findViewById(R.id.imageView); Intent i = getIntent(); Bundle b = i.getExtras(); assert b != null; receivedCheckValue = b.getBoolean(MainActivity.CHECK_YES_OR_NO); if (b != null){ newString = null; } else if(receivedCheckValue == true){ myRandInt = 1; t.setText("Your Number is 1"); v.setImageResource(R.drawable.diceone); }` Is there something wrong with it? – Sam Biner Oct 03 '18 at 20:25
  • 1) You should update your question with your code nicely formatted for readability. Adding code as a comment is almost impossible to read. 2) When you update your question, add more detail than "it is totally messing up". Explain your issue like you're talking to a 5-year-old. https://stackoverflow.com/help/how-to-ask – dominicoder Oct 03 '18 at 22:09