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.
Asked
Active
Viewed 86 times
0
-
Check this out.. I hope it will help you http://stackoverflow.com/questions/2776116/how-do-i-dynamically-choose-which-activity-to-launch-when-opening-an-app – Mayank Pandya Aug 27 '16 at 07:46
-
When you say "If you select A - activity A displayed. If you select B - activity B displayed" you mean the XML of those activities? – We're All Mad Here Aug 27 '16 at 07:47
-
@We'reAllMadHere yes – SummerAugust Aug 27 '16 at 07:51
-
Why don't you use a fragment and replace it on radio button click? That would be the proper way. – Prokash Sarkar Aug 27 '16 at 07:52
-
Then I guess you could use something like that `Intent i = new Intent(that, Activity_B.class);` `startActivity(intent);`. What that does is, when something you want occurs, it changes activities and goes from your current to activity B – We're All Mad Here Aug 27 '16 at 08:02
2 Answers
1
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))
}
}
});

Prokash Sarkar
- 11,723
- 1
- 37
- 50
0
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();

Shunan
- 3,165
- 6
- 28
- 48