4

I really find this weird, I'm able to get other values except one. Here's my code:

Intent intent = new Intent(this.context, SecondActivity.class);
intent.putExtra("contact", n.getContact());
intent.putExtra("email", n.getEmail());
intent.putExtra("address", n.getAddress());
intent.putExtra("test", "hello world");
context.startActivity(intent);

And this is the code where I'll get the values under OnCreate():

Intent intent = getIntent();
contact = intent.getStringExtra("contact");
email = intent.getStringExtra("email");
address = intent.getStringExtra("address");
test = intent.getStringExtra("test");

Everything works well except for the String test. It will always give me null value. Any solution for this?

Question added: Does intent.putExtra() doesn't accept quoted text anymore on the second parameter? LOL

Glen
  • 79
  • 2
  • 9
  • 1
    Are you sure n.getContact and other methods are not returning null? – crashOveride Apr 14 '16 at 06:09
  • Pretty sure. That's why I was wondering why that specific line returns null. – Glen Apr 14 '16 at 06:13
  • It is giving me a value. Just tried it recently. This is really weird, I've been doing this before. I don't know why that line gives me null. – Glen Apr 14 '16 at 06:18
  • I am wondering aboput the description in API: `Add extended data to the intent. The name must include a package prefix, for example the app com.android.contacts would use names like "com.android.contacts.ShowAll".` I never added a package prefix when I have done it but it worked....what do they mean with that? – Opiatefuchs Apr 14 '16 at 06:21
  • have you tried to change the order? I mean, just switch the last statements, is it still `test`you don´t get then or will it be `address`? Try it... – Opiatefuchs Apr 14 '16 at 06:25
  • I tried switching last two lines on both putExtra and getStringExtra but still the same. I can retrieve the `address` but not the `test`. Is this some kind of a glitch? I've been doing this before and it works fine but now it's not. – Glen Apr 14 '16 at 06:34
  • there must be elswhere some issue that you did not see I guess. Maybe you have changed one little detail but it´s hidden like a mystical beeing. Believe me, I stumpled about that s..t in the beginning often....So maybe you should post all relevant code that has something to do with that. – Opiatefuchs Apr 14 '16 at 06:49
  • Have you done this before also with the key `test` ? And what if you change `test` to another key for example `MY_KEY`.....did you then get `hello world` ? – Opiatefuchs Apr 14 '16 at 06:49
  • @Opiatefuchs i think adding package prefix is to avoid any mix/overwrite of data between apps, as the intent could be coming from external apps or even from the framework, so same-name key is something that could happen... just a guess :) – Yazan Apr 14 '16 at 07:25
  • @Yazan...yes...sure (slapping hand against my head)...I haven´t thought about but it´s logical... :) – Opiatefuchs Apr 14 '16 at 07:45
  • I have a similar problem when i want to put a html source code as string to intent. I think some special characters like quotes cause problem.. or there is a limit for string length.. i solve problem by putting filepath instead of html source code – mDonmez Aug 06 '20 at 04:46

4 Answers4

1

In MainActivity.class

Intent myIntent=new Intent(MainActivity.this, ResultActivity.class);
Bundle bundle=new Bundle();
bundle.putString("contact", n.getContact());
bundle.putString("email", n.getEmail());
bundle.putString("address", n.getAddress());
bundle.putString("test", "hello world");
myIntent.putExtra("MyPackage", bundle);
startActivity(myIntent);

In ResultActivity.class

Intent callerIntent=getIntent();
Bundle packageFromCaller=
callerIntent.getBundleExtra("MyPackage");
String contact =packageFromCaller.getString("contact");
String email =packageFromCaller.getString("email");
String address= packageFromCaller.getString("address");
String test= packageFromCaller.getString("test");

Hope. It will help you !!!

Nguyễn Trung Hiếu
  • 2,004
  • 1
  • 10
  • 22
1

You need to add .toString() and the end of the method call on each putExtra

Intent intent = new Intent(this.context, SecondActivity.class);
intent.putExtra("contact", n.getContact().toString());
intent.putExtra("email", n.getEmail().toString());
intent.putExtra("address", n.getAddress().toString());
intent.putExtra("test", "hello world");
context.startActivity(intent);
Miguel Tomás
  • 1,714
  • 1
  • 13
  • 23
0

use getApplicationContext() instead of this.context and use startActivity instead of context.startActivity

First Activity

Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
intent.putExtra("test", "hello world");
startActivity(intent);

Second Activity

Intent intent = getIntent();
intent.getStringExtra("test");
Omar Vodiak
  • 116
  • 3
-1

send data

Intent intent  = new Intent(this.context,SecondActivity.class);
                intent.putExtra("contact", n.getContact());
                startActivity(intent);

get data

Bundle data = getIntent().getExtras();
contact = data.getString("contact");
Nirav Ranpara
  • 13,753
  • 3
  • 39
  • 54
  • Please read my explanation. I know how to pass data. You should not use `Bundle` since we're using `Intent` to put some data. – Glen Apr 14 '16 at 06:15