Reputation: 3
private RadioGroup rgTravel;
SharedPreferences sharedPreferences;
private static final String PREFS = "PREFS";
private static final String Travel = "travel";
private RadioButton radioButton;
String travel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
final Button btnOk = (Button)findViewById(R.id.btnOk);
btnOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent activity3 = new Intent(MainActivity2Activity.this, MainActivity3Activity.class);
rgTravel = (RadioGroup) findViewById(R.id.rgTravel);
int selectedId = rgTravel.getCheckedRadioButtonId();
radioButton = (RadioButton) findViewById(selectedId);
travel = radioButton.getText().toString();
SharedPreferences preferences-= PreferenceManager.getDefaultSharedPreferences(MainActivity2Activity.this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(Travel, travel);
editor.commit();
Toast.makeText(getApplicationContext(), travel, Toast.LENGTH_LONG).show();
startActivity(activity3);
}
});
}
This is my activity2, where I'm supposed create the sharedpreference, and save the user's choice. (I want save the choice of the user in a radiogroup) See my activity 3 where I'm supposed get back the choice :
public void onCreate(Bundle savedInstanceState) {
String choice;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity3);
SharedPreferences PREFS = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
choice = PREFS.getString("Travel", "Beach");
Toast.makeText(getApplicationContext(), choice, Toast.LENGTH_LONG).show();
displayPicture(choice);
Button next = (Button) findViewById(R.id.button3);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent activity4 = new Intent(MainActivity3Activity.this, MainActivity4Activity.class);
startActivity(activity4);
}
});
}
I have created a Toast in each activity in order to be sure that the problem is really with the preference and not with another step.
Upvotes: 0
Views: 41
Reputation: 37404
It should be travel
not Travel
because it is case sensitive
Use in next activity
choice = PREFS.getString("travel", "Beach");
instead of
choice = PREFS.getString("Travel", "Beach");
Upvotes: 5