user2490448
user2490448

Reputation: 45

what wrong with putExtra and string values

Ok....what's wrong with this? Ive made a button...added this to the intent:

intent.putExtra("test","test");

then in onReceive had a little problem:

Bundle extras = intent.getExtras();
Log.v("Test", "Ok lets see");
String t=extras.getString("test");
Log.v("Test", "t="+t);
if(t=="test") t="tada";
else t="test";
Log.v("Test", "Ok...t="+t);

Now see the results from log

07-02 23:48:24.195    6278-6278/com.example.widget             V/Test: Ok lets see
07-02 23:48:24.205    6278-6278/com.example.widget             V/Test: t=test
07-02 23:48:24.205    6278-6278/com.example.widget             V/Test: Ok...t=test

Took me 1-2 hours to find out what was wrong with my code. Of course i have no idea what is wrong with the example above. My solution was to change from string to numbers (byte in my case). So after that everything was ok.

Upvotes: 0

Views: 84

Answers (1)

Jacob
Jacob

Reputation: 995

You're comparing Strings using ==, which only works if they refer to the exact same object.

Use the equals method instead.

if(t.equals("test"))
    t = "tada";

Log.v(t) //Logs tada

See this answer for more info.

Upvotes: 3

Related Questions