1

On my start(home) screen I have 3 buttons. Button_1, Button_2 and Button_3. They are all open same Activity B. So here I need depending on which button is clicked on home screen to open different Activity C after second activity. I can make it with 2 buttons but when I try with third button with elseif it doesn't work properly. Here is how I trying it. Home screen

Button_1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, SecondActivity.class); intent.putExtra("isButtonClicked",true); startActivity(intent); } }); Button_2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, SecondActivity.class); intent.putExtra("button", true); startActivity(intent); } }); Button_3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, SecondActivity.class); intent.putExtra("isButtonClicked",false); startActivity(intent); } }); 

Here is SecondActivity

boolean isButton = getIntent().getBooleanExtra("isButtonClicked",false); boolean button = getIntent().getBooleanExtra("button",true); if(isButton) { Intent newActivity = new Intent(SecondActivity.this, ThirdActivity.class); newActivity.putExtra("Position", Position); newActivity.putExtra("resultServer", resultServer); newActivity.putExtra("text", MyArrList.get(position).get("text").toString()); newActivity.putExtra("name", MyArrList.get(position).get("name").toString()); startActivity(newActivity); } else if (button) { Intent newActivity = new Intent(SecondActivity.this, FourthActivity.class); newActivity.putExtra("Position", Position); newActivity.putExtra("resultServer", resultServer); newActivity.putExtra("id", MyArrList.get(position).get("id").toString()); startActivity(newActivity); } else { Intent newActivity = new Intent(SecondActivity.this, FifthActivity.class); newActivity.putExtra("Position", Position); newActivity.putExtra("resultServer", resultServer); newActivity.putExtra("id", MyArrList.get(position).get("id").toString()); startActivity(newActivity); } 

As you can see after second activity which is same for all 3 buttons then I want to load different. If I put it just with if{}else{} i.e. for two buttons is working good. But now I get same result on Button_2 and Button_3.

2
  • same result on button 2 and 3... which block ? Commented Nov 29, 2014 at 19:24
  • elseif{}else{} ... it is open FifthActivity.class for both buttons. Commented Nov 29, 2014 at 19:25

2 Answers 2

3

Just found that:

boolean button = getIntent().getBooleanExtra("button",true); 

should always return true. As it has a default value set to true. So your elseif will work always..if Button_1 is not clicked!

Fix:

boolean button = getIntent().getBooleanExtra("button",false); 
Sign up to request clarification or add additional context in comments.

2 Comments

Ok, how can I fix this?
change that true to false for default value.
2

You have:

boolean button = getIntent().getBooleanExtra("button",true); 

So in 2nd and 3rd option this value will be true, so this is the reason of it.

Change to:

boolean button = getIntent().getBooleanExtra("button",false); 

and should be ok.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.