passing of parameters between activities

0

I would like to be able to use the following code:

public void Green(View view) {
        String cor3 = "verde";

        Bundle parametros = new Bundle();
        parametros.putString("verde",cor3);
        Intent it =  new Intent(this, Main3Activity.class);
        parametros.putString("verde",cor3);
        it.putExtras(parametros);
        startActivity(it);

2nd screen

Intent intent = getIntent();        
String verde = intent.getStringExtra("verde");

 if ( cor=="verde"){
            btn1.setBackgroundColor(Color.GREEN);
            btn2.setBackgroundColor(Color.GREEN);
            btn3.setBackgroundColor(Color.GREEN);
            btn4.setBackgroundColor(Color.GREEN);
    }

I run the emulated and do not funfa

    
asked by anonymous 25.09.2017 / 19:26

2 answers

1

The string comparison in Java must be done by the equals method.

if (cor.equals("verde")) { ...

A link here from stackoverflow explaining: Comparing Strings in Java

    
25.09.2017 / 19:46
0

Try this:

1st Screen.

public void Green(View view) {
Intent it =  new Intent(this, Main3Activity.class);
it.putExtra("verde",cor3);
startActivity(it);
}

And to recover:

2nd Screen.

Intent it = getIntent();
String color;
Bundle bd = it.getExtras();       
if(bd != null)
{
   color = (String) bd.get("verde");           
}

if ( color.equals("verde")) // EDITED
{
   btn1.setBackgroundColor(Color.GREEN);
   btn2.setBackgroundColor(Color.GREEN);
   btn3.setBackgroundColor(Color.GREEN);
   btn4.setBackgroundColor(Color.GREEN);
}
    
25.09.2017 / 19:35