0

I have a lot of strings in my strings.xml, I want to putExtra to another intent where the data that I retrieve is taken from strings.xml, but when the application is run it doesn't get the results I want

sender activity

Intent moveIntent = new Intent(MainActivity.this, informationActivity.class);
moveIntent.putExtra(informationActivity.data1, "@string/THISISMYSTRINGFROMSTRINGSXML");
startActivity(moveIntent);

recipient intent

String data1= getIntent().getStringExtra(data1);
text1.setText(data1);

and the results that I got were

@string/THISISMYSTRINGFROMSTRINGSXML

I think by providing data like the above will make the text that appears taken from the strings.xml file, apparently not, is there a way to fix it?

dimaz
  • 43
  • 2
  • use fun for getting the res -> for string use `getString(R.string.THISISMYSTRINGFROMSTRINGSXML)` – P.Juni Aug 08 '19 at 08:16

4 Answers4

8

Get the string by:

getResources().getString(R.string.string_name)
1

Use getString method to fetch the actual string. Like below

String str = getString(R.string.<string-name>);
putString(<key>, str);

Then fetch using

intent.getString(<key>)

Hope this helps

TheGraduateGuy
  • 1,450
  • 1
  • 13
  • 35
0

@string/foo returns an int which is basically just a reference. To get an exact string value, use:

context.getString(R.string.string_name);

If you're implementing this inside a class that extends Activity or its subclass, you can do it this way.

getString(R.string.string_name);

I hope this helps. Merry coding!

Taslim Oseni
  • 6,086
  • 10
  • 44
  • 69
0
Intent moveIntent = new Intent(MainActivity.this, informationActivity.class);
moveIntent.putExtra(informationActivity.data1,getResources().getString(R.string.string_name));
startActivity(moveIntent);
Praveen
  • 430
  • 3
  • 11