TMA-1
TMA-1

Reputation: 84

Passing data between activities

My main activity contains a EditText and a button. I would like to send whats written in the EditText to the next activity started by pressing the button.

In the first activity I have this:

        Intent i = new Intent(firstActivity.this, secondActivity.class);  
        i.putExtra("myString", theEditText.getText());
        startActivity(i);

So far so good. In the second activity I use this:

  Bundle bundle = getIntent().getExtras(); 
  myRecivedString = bundle.getString("myString")

But here myRecivedString turns up empty.

Any suggestions?

Upvotes: 0

Views: 259

Answers (3)

Sumant
Sumant

Reputation: 2795

Just use theEditText.getText().toString();. You are able to get the text written in edittext.

Upvotes: 1

pankajagarwal
pankajagarwal

Reputation: 13582

change i.putExtra("myString", theEditText.getText()); to i.putExtra("myString", theEditText.getText().toString());

Upvotes: 0

Kartik Domadiya
Kartik Domadiya

Reputation: 29968

See getText() method of EditText returns object of Editable class. So if you want to pass the string contained in EditText, you have to use toString().

See the example here :

 thisEditText.getText().toString();

So the code in your first activity should look like :

 Intent i = new Intent(firstActivity.this, secondActivity.class);  
 i.putExtra("myString", theEditText.getText().toString());
 startActivity(i);

Upvotes: 2

Related Questions