Reputation: 27
How I can display another activity instead of the main, if in the settings, in the list of RadioButtons marked a particular item? For example: If you select A - activity A displayed. If you select B - activity B displayed. If you select C - activity C displayed.
Upvotes: 0
Views: 92
Reputation: 11873
First of all, get which button is currently selected from the radio group. After you get the current radio button, simply check the button's text or a tag and launch the activity based on it.
Try something like this,
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
// get selected radio button from radioGroup
int selectedId = radioGroup.getCheckedRadioButtonId();
// find the radio button by returned id
radioButton = (RadioButton) findViewById(selectedId);
// toggle views on radio button click
if (radioButton.getText().toString().equals("Activity A") {
startActivity(new Intent(CurrentActivity.this, ActivityA.class))
}
else if (radioButton.getText().toString().equals("Activity B")) {
startActivity(new Intent(CurrentActivity.this, ActivityB.class))
}
else if (radioButton.getText().toString().equals("Activity C")){
startActivity(new Intent(CurrentActivity.this, ActivityC.class))
}
}
});
Upvotes: 1
Reputation: 3254
You can use sharedPreferences. Just store A in sharedPreferences if 1st radio button is selected, B if 2nd and so on. While creating an intent, you can check what is stored in CallingActivity SharedPreference, If its default value then call main, otherwise use switch case to call specific activity.
SharedPreferences sharedPref = getSharedPreferences("CallingActivity", Context.MODE_PRIVATE);
SharedPreferences.Editor sEditor = sharedPref.edit();
sEditor.putString("ActivityName","A");
sEditor.commit();
Upvotes: 0