Im coding a little Android-App at the moment and discovered a strange issue regarding multiple Buttons. I have an Activity with 4 Buttons on it. When I press multiple Buttons at once, both actions are executed. So I can press all 4 Buttons and all 4 following Activities are started.
This is my onButtonClick-Method
public void onButtonClick(View view) {
Intent intent = new Intent(this, RunActivityConfirm.class);
switch(view.getId()) {
case R.id.btnRunAcceleration:
intent.putExtra("DisciplineName", "Acceleration");
startActivity(intent);
break;
case R.id.btnRunSkidPad:
intent.putExtra("DisciplineName", "Skid Pad");
startActivity(intent);
break;
case R.id.btnRunAutocross:
intent.putExtra("DisciplineName", "Autocross");
startActivity(intent);
break;
case R.id.btnRunEndurance:
intent.putExtra("DisciplineName", "Endurance");
startActivity(intent);
break;
}
}
First I thought the problem ist that I always call startActivity() in every single “case” but even if I try the following all 4 Activities get started at the same time
public void onButtonClick(View view){
Intent intent = new Intent(this, RunActivityConfirm.class);
switch(view.getId()) {
case R.id.btnRunAcceleration:
intent.putExtra("DisciplineName", "Acceleration");
break;
case R.id.btnRunSkidPad:
intent.putExtra("DisciplineName", "Skid Pad");
break;
case R.id.btnRunAutocross:
intent.putExtra("DisciplineName", "Autocross");
break;
case R.id.btnRunEndurance:
intent.putExtra("DisciplineName", "Endurance");
break;
}
startActivity(intent);
}
This happens all over my App. No matter which Button I press every associated Action is executed and every associated Activity is started.
Is there anything I can do about it?
This is expected behavior. Your code starts an activity when a button is pressed. Multiple buttons are pressed, so multiple activities are started.
If you want to change this so that only one button can be pressed, then you need to add code to do so. You could disable the other buttons in
onButtonClick(and re-enable them when appropriate).