49

This is how strings are being added to Extras:

Intent i = new Intent();
i.putExtra("Name", edt_name.getText());
i.putExtra("Description", edt_desc.getText());
i.putExtra("Priority", skb_prior.getProgress());
setResult(RESULT_OK, i);
finish();

This is how I try to extract them in onActivityResult():

String name = data.getStringExtra("Name");
String desc = data.getStringExtra("Description");
int prior   = data.getIntExtra("Priority", 50);

But after the second code block name and desc are null's, though prior has it's proper value. Moreover, in debugger I can see, that data.mExtras.mMap contains needed Strings, but only after first request to it.

hotkey
  • 140,743
  • 39
  • 371
  • 326

1 Answers1

104

When you insert your Extras trying adding .toString()

i.putExtra("Name", edt_name.getText().toString());

You are seeing the CharSequence value in there but you need to convert it to a String to call getStringExtra(). Obviously, just do this for the Strings. You see the correct value for your int because that is done correctly

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • 1
    Thanks, it works, as well as `"" + edt_name.getText()`, which looks not as pretty :) – hotkey Mar 21 '13 at 19:01
  • 2
    It works because `""` is a String literal and you're adding to it. It works the same way as `"" + 2` comes out to `"2"`. Whatever is being added to the literal is converted to a String via their own class's `toString` function. – Michael Celey Mar 21 '13 at 21:01
  • @codeMagic, Why do you say that it's an `object`? Isn't it `CharSequence`? – Pacerier Nov 17 '14 at 03:38
  • @Pacerier Yes, it would actually return a [charSequence](http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.3_r1/android/widget/TextView.java#TextView.getText%28%29) – codeMagic Nov 20 '14 at 13:40
  • @codeMagic In my case, issue is not this one though, as I tried to use same receiver for different Alarms this issue triggered, don't know why? but I end up adding each individually it worked, the purpose of intent in this new approach is now gone.. – Naga Nov 16 '18 at 09:11