passing parameters between activitys

Asked

Viewed 51 times

0

could help me with this android studio code ...I do not cosnigo recover the string to run if:

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 spin the emulated and not funf

  • The problem is that you are receiving the value in the variable verde and in the if is using the variable cor. On the other hand it should use, for comparison, the method equals() and not ==.

2 answers

1

String comparison in Java should be done by the method equals.

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

A link here from stackoverflow explaining: How to compare Strings in Java

  • Not necessarily need to be compared this way @Paulo C, it goes from the usual developer...

  • ok.. now it was .. thank you very much!

  • @Matheus, from which version of java the string comparison can be direct with the ==?

  • What is the relevance of this question ??

  • Just curious. Because String is an object, I thought it was only with equals that the comparison could be made.

  • 1

    So Paul kind hehe puts in your head that everything is extends the Object class, like if you do this String color = "Green"; Object obj = color; it will work, but it will be an Object, with everything for you to get the String value you would have to use a cast to transform itla from Object to string. As for your question, in the java language we have always been able to compare String in this way.

  • @Matheus always gave and gives. The problem is that it doesn’t always give. So, if you want to know value of two String are equal use equals(). The use of == should be used to check if it is the same object(reference).

  • @ramaral you who are a more experienced user in java hehe is the exact guy to answer this question, however Paul C, you do not allow comparing strings of different instances, type like String str = "test"; String str2 = new String("test"); (str == str2) // false understands ?

Show 3 more comments

0

Try it that way:

1° Canvas.

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

And to recover:

2° Canvas.

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);
}
  • Ok, it worked ...the problem was really in the java comparison...

  • @Rafaeluemura se foi concluida a resposta, mark as correct... Thanks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.