Anwar
Anwar

Reputation: 4246

Passing variables through activities

I'm making an apps and I need to pass variables between two different activites. I got a TextEdit, in which I save the entry to pass it to another activity. Here's my code :

Activity "textEdit" (let call it like this...) :

if (editText_descriptionHomework.getText().toString().matches("")){
                    Toast.makeText(getApplicationContext(), messErr_noInput, Toast.LENGTH_LONG).show();
                }
                else {
                    Intent i = new Intent(getApplicationContext(), MainActivity.class);
                    String descr = editText_descriptionHomework.getText().toString();
                    i.putExtra(description, descr);

                    finish();

                    startActivity(i);

                }

And here's the "main activity" (in which I want to display this data) :

Bundle extras = getIntent().getExtras();
        if (extras != null) {
            String value = extras.getString("description");
            Toast.makeText(getApplicationContext(), value, Toast.LENGTH_LONG).show();
        }

So, if I put nothing, I correctly have the toast saying "error blablabl", but if I put something, and I click on the button which trigg the "textEdit activity" code, it display nothing (black toast with no caracters).

Did I write something wrong guys ?

Upvotes: 1

Views: 59

Answers (3)

danidee
danidee

Reputation: 9624

it should be like this

i.putString("description", descr);

you forgot the quotes

Upvotes: 1

Jayo2k
Jayo2k

Reputation: 249

first it is better to replace this :

if (editText_descriptionHomework.getText().toString().matches(""))

by

if (editText_descriptionHomework.getText().toString().idEmpty())

that way you are making sure the EditText is empty, .match is like preg_match but for android Now to get the value, try this instead:

String value = getIntent().getExtrasString("description");

or

String value = getIntent().getExtras().getString("description");

Last replace :

i.putExtra(description, descr);

by

i.putExtra("description",descr);

Upvotes: 1

alpinescrambler
alpinescrambler

Reputation: 1954

It should be:

i.putExtra("description", descr);

Yours doesn't have the quotes...

Upvotes: 3

Related Questions