0

I am currently attempting to pass a string across activities. The string is used to set an image in the second activity. So the user is clicking a button in the first activity and then the appropriate image is loaded in the second activity based upon what was selected. There are three buttons in the first activity all which should send a String value to the next activity.

This is what I have in my first activity:

Button archerButton = (Button) findViewById(R.id.Button_Archer);
archerButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        String selection = "archer";
        Bundle basket = new Bundle();
        basket.putString("key", selection);
        Intent i = new Intent(SelectCharacterActivity.this, LevelOneActivity.class);
        i.putExtras(basket);
        startActivity(i);
        finish();
        }
    });
}

The receiving activity has the following code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    setContentView(R.layout.level_one);

    ImageView img = (ImageView) findViewById(R.id.Character_Chosen);

    Bundle extras = getIntent().getExtras();
    String Toon = extras.getString("key");

    if(Toon=="archer"){
        img.setImageResource(R.drawable.archer);
    }

    if(Toon=="mage"){
        img.setImageResource(R.drawable.mage);
    }
    if(Toon=="warrior"){
        img.setImageResource(R.drawable.warrior);
    }
}     

I know that the receive if statements section works because if I manually set the value Toon the image will load. So somewhere the information is not being sent or read properly. I followed a tutorial online and but am stumped on this.

Rhendar
  • 410
  • 7
  • 25

5 Answers5

2

You are comparing your strings wrong,

Toon=="Anything"

will never be true, you need to use

Toon.equals("Anything")

for it to work.

EDIT:

This is true because "==" will just compare the memory addresses of the Strings, and "Anything" will just have been allocated and definitely won't match Toon. .equals however actually compares the chars in the string.

Also to test things like this try putting in a

Log.i("YourAppName", "Toon value is: " + Toon);

after

String Toon = extras.getString("key");
bclymer
  • 6,679
  • 2
  • 27
  • 36
2

issue is Java 101. string equality test is done using the equals method.

Toon.equals("archer");

Extra tip : inverse your test to avoid null pointer exception :

"archer".equals(Toon);

Extra extra tip : considering your cases are mutually exclusive, use if else if to avoid testing all cases.

njzk2
  • 38,969
  • 7
  • 69
  • 107
0

try to use SharedPreferences, Set it after/with the button-click and get it on 2. Activity OnCreate

Eveli
  • 498
  • 1
  • 6
  • 27
0

If you just want to pass a simple String, you can use the following:

On sending:

    Intent i = new Intent(SelectCharacterActivity.this, LevelOneActivity.class);
    i.putExtra("key", selection);
    startActivity(i);

On receiving:

    String selection = getIntent().getStringExtra("key");

If there is no parameter with this key, selectionwill be null.

Safime
  • 201
  • 1
  • 8
0

Try this

Intent i = new Intent(FirstActivity.this, SecondActivity.class); i.putExtra("email", "lol@hotmail.com"); startActivity(i);

and

Intent i = getIntent(); String myemail = i.getStringExtra("email");

Furthermore, to compare String, use .equals() not ==

MrYanDao
  • 1,253
  • 1
  • 15
  • 27