Reputation: 29
I'm working on the game, when I want the user to be able to switch on hints. I have a switch button in my MainActivity and I want to pass the value when the button is switched ON, to the GameActivity and there code what I need. I have been trying to do this for some time now and couldn't find answer to this.
public void passSwitch(View view){
SharedPreferences.Editor editor = getSharedPreferences(
"com.quiz.myquiz", Context.MODE_PRIVATE).edit();
editor.putBoolean("state", hintSwitch.isChecked());
editor.apply();
}
I'am calling this method on the xml onClick.
Upvotes: 0
Views: 413
Reputation: 1205
This is the easy way to pass data between Activity's
Intent intent = new Intent(getBaseContext(), GameActivity.class);
intent.putExtra("state", true);
startActivity(intent);
Then in your GameActivity
Boolean state = getIntent().getExtras().getBoolean("state");
Upvotes: 2
Reputation: 737
Since you're using save preferences, simply call back the values where necessary like
getSharedPreferences("com.quiz.myquiz", Context.MODE_PRIVATE).getBoolean("state", false);
Upvotes: 3
Reputation: 83527
Your passSwitch()
method saves the setting. Now you need to load the setting when GameActivity
starts.
Alternatively, MainActivity
can pass the setting in the Intent which you use to start GameActivity
.
Upvotes: 0