Passing Value from string.xml from one Fragment to another activity? - android

The value in string.xml file is.
<string name="hello_blank_fragment">Hello blank fragment</string>
From this Fragment i am trying to pass value to another activity ... by using putextra
i=new Intent(Basic.this.getContext(),DisplayBasicData.class);
i.putExtra("header", R.string.action_settings);
startActivity(i);
Then i try to get value using
String text_point = getIntent().getStringExtra("header");
But it not getting the value.

Try use Bundle :
Bundle extras = new Bundle();
extras.putString("header", getResources().getString(R.string.action_settings));
i=new Intent(Basic.this.getContext(),DisplayBasicData.class);
i.putExtras(extras);
startActivity(i);
and extract in DisplayBasicData activity:
String text_point = getIntent().getExtras().getString("header");

You are sending resource id ,not string
Here you can get string in two ways :
First way:
i.putExtra("header", R.string.action_settings);
int text_pointResourceId = getIntent().getIntExtra("header");
String text_point = getResources().getString(text_pointResourceId)
secondWay:
i.putExtra("header", getResources().getString(R.string.action_settings));
String text_point = getIntent().getStringExtra("header");
Hope it helps
Instead of sending data directly , it is better to use Bundle

you can pass that string simply in following way, no need of putting data in bundle.
Intent intent = new Intent(getActivity(), DisplayBasicData.class);
String myText = getActivity().getResources().getString(R.string.action_settings);
intent.putExtra("header", myText);
startActivity(intent);
then you can retrieve that data by using
String text_header = getIntent().getStringExtra("header");

Related

Not receiving all values sent by putextra()

I tried to send data from one activity to another. The problem is I am not receiving all the datas. Just 3 instead of 6.PS: I am new to android development
sending data from this
Intent i=new Intent(getActivity(),PigeonInfo.class);
String n=p.getPigeonID();
String f=p.getFathersID();
String m=p.getMothersID();
String g=p.getGender();
String gr=p.getGroup();
String u=p.getPicURL();
i.putExtra("PID",n);
i.putExtra("FID",f);
i.putExtra("MID:",m);
i.putExtra("PGN:",g);
i.putExtra("PGR:",gr);
i.putExtra("PUR",u);
startActivity(i);
To this:
Intent i=getIntent()
e1.setText(i.getStringExtra("PID"));
e2.setText(i.getStringExtra("PGR"));
e3.setText(i.getStringExtra("PGN"));
e4.setText(i.getStringExtra("FID"));
e5.setText(i.getStringExtra("MID"));
String url= i.getStringExtra("PUR");
Use getIntent() instant of intent for getting the intent value
`
first Activity
Intent i=new Intent(getActivity(),PigeonInfo.class);
String n=p.getPigeonID();
String f=p.getFathersID();
String m=p.getMothersID();
String g=p.getGender();
String gr=p.getGroup();
String u=p.getPicURL();
i.putExtra("PID",n);
i.putExtra("FID",f);
i.putExtra("MID:",m);
i.putExtra("PGN:",g);
i.putExtra("PGR:",gr);
i.putExtra("PUR",u);
startActivity(i);
Second activity
Intent inent = getIntent();
e1.setText(intent.getStringExtra("PID"));
e2.setText(intent.getStringExtra("PGR"));
e3.setText(intent.getStringExtra("PGN"));
e4.setText(intent.getStringExtra("FID"));
e5.setText(intent.getStringExtra("MID"));
String url= intent.getStringExtra("PUR");`
For these 3 lines you add an extra : to the end of the key:
i.putExtra("MID:",m);
i.putExtra("PGN:",g);
i.putExtra("PGR:",gr);
But when you retrieve them from the intent, you don't have a : anymore in the key:
e5.setText(intent.getStringExtra("MID"));
So you can fix this by removing the extra : characters in your putExtra(...) calls.
Having typo's in these keys is pretty commons. You can work around such issues by defining these keys in static fields, which you reference from both places:
class Keys {
public static final String PIGEON_MID = "pigeon_mid"
}
...
intent.putExtra(Keys.PIGEON_MID, pigeon.getMid());
...
String mid = intent.getStringExtra(Keys.PIGEON_MID);

how to send a string to listView in another activity ? in android

Good evening guys,
I'm making an app and I want to know how to send a string to "List-View" in another activity ?
You can send data using the following code -
Intent intent = new Intent(this,newActivity);
intent.putExtra(name, value)
name = The name of the extra data
value = The String data value.
startActivity(intent);
In the new activity, you receive string via following (in onCreate)
Intent intent = getIntent();
String str = intent.getString(name)
name = The name of the extra data
Now search the web on how to add a string to list view. You will find it easily

Android: Adding more than one Bundle to a new activity

I'm trying to pass in two separate pieces of information to a new activity in my Android application.
I currently have this:
Bundle dataBundle = new Bundle();
Bundle extras = getIntent().getExtras(); // student id
dataBundle.putInt("id", 0);// lesson id
Intent intent = new Intent(getApplicationContext(),com.example.ltss.dyslexia.app.LessonNotes.class);
intent.putExtras(dataBundle);
intent.putExtras(extras);
startActivity(intent);
I then have the code accessing this information. However, adding the second bundle overrides the first one.
Bundle extras = getIntent().getExtras();
Bundle studentId = getIntent().getExtras();
Log.d("LessonID: ", String.valueOf(extras));
Log.d("StudentID: ", String.valueOf(studentId));
I need to have the information passed in separately as I need to check if one of them is null.
Can what i'm asking be done? Any ideas as to how to do this? Or another way to do this? (parsing maybe?)
Thanks
you could use putExtra("bundle1", bundle1) and putExtra("bundle2", bundle2) and then use getIntent().getBundleExtra("bundle1"); and getIntent().getBundleExtra("bundle2"); to retrieve both
Maybe you're over thinking this. You can put a ton of information in 1 bundle.
Bundle bundle = new Bundle();
bundle.putString("studentid", "Student0983");
bundle.putInt("lessonid", 0);
bundle.putString("moreinfo", "needed some extra data on that student");
bundle.putInt("studentincome", 4250);
Intent intent = new Intent(getApplicationContext(),com.example.ltss.dyslexia.app.LessonNotes.class);
intent.putExtras(bundle);
startActivity(intent);
Now to get that data in the new activity
Bundle bundle = getIntent().getExtras();
Log.d("studentid: ", bundle.getString("studentid"));
Log.d("lessonid: ", bundle.getInt("lessonid"));
Log.d("moreinfo: ", bundle.getString("moreinfo"));
Log.d("studentincome: ", bundle.getInt("studentincome"));

Anyway to have more than one extra added to intent?

I want to have more than one extra added to Intent. One to hold a double and one to hold long. Is this possible?
If so, how would I do it and how would I get the information from each extra?
You can add as many extras to an Intent as your heart desires, they are all just key value data:
Intent intent = new Intent();
intent.putExtra("name", "MyName");
intent.putExtra("age", 35);
intent.putExtra("weight", 155.6);
And they can be retrieved using the same key names:
String name = intent.getStringExtra("name");
int age = intent.getIntExtra("age", 0);
double weight = intent.getDoubleExtra("weight", 0.0);
intent.putExtra(#ExtraDoubleKey, #ExtraDoubleValue);
intent.putExtra(#ExtraLongKey, #ExtraLongValue);
Where #ExtraDoubleKey is a string that you will use to access the extra (i.e. "price" or something), and #ExtraDoubleValue is the value of the extra (the double variable you wish to pass). Similarly for #ExtraLongKey and #ExtraLongValue.
Then to access the extras in your next activity you can use:
double doubleValue = getIntent().getExtras().getDouble(#ExtraDoubleKey);
long longValue = getIntent().getExtras().getLong(#ExtraLongKey);
to get the value of the double extra with the key #ExtraDoubleKey.
https://stackoverflow.com/a/11461530/776075
Devunwired is correct.
But the way i see is
you can keep only one value per Type.
Like one string, one int, one double etc..
You are not capable of containing 2 string values. or two integers.
I have experienced this on a program and i have overcome it by using
one string and one Boolean.
You can use Bundle and pass it as a parameter to the Intent.
Intent nextActivity = new Intent(this, Activity2.class);
Bundle passData = new Bundle(); //to hold your data
passDataBndl.putString("fname", fname); //put in some String. the first parameter to it is the id, and the second parameter is the value
nextActivity.putExtras(passDataBndl); //Add bundle to the Intent
startActivityForResult(nextActivity, 0); //Start Intent

NullPointerException from getIntent().getExtras().getString("to")

InboxDetailActivity.java:
Intent i = new Intent(InboxDetailActivity.this,Compose.class);
Bundle b = new Bundle();
b.putString("To", ConstantData.inbox_from);
Log.d("From Value", ConstantData.inbox_from);
b.putString("Subject", "RE:" + ConstantData.inbox_subject);
Log.d("Subject Value", ConstantData.inbox_subject);
b.putString("FromId", ConstantData.inbox_fromid);
Log.d("From Id Value",ConstantData.inbox_fromid);
i.putExtras(b);
startActivity(i);
Compose.java:
Intent i = getIntent();
Bundle b = i.getExtras();
to = b.getString("To");
subject = b.getString("Subject");
toId = b.getString("FromId");
I am getting NullPointerException at to = b.getString("To");
Bundle b = i.getExtras();
getExtras() returns null.
Agree with John's answer adding possible solution.
What you are doing is create a bundle , insert values in this and then pass this bundle.
And than you just fetch all the values one by one using its keys.
I am working with bundles but I simply add desired values directly using putExtra method. And I haven't got any problem till date. I recommend you to use put extra and check whether it works or not.
I would like to know what makes you to apply this way for bundles? Have you just read it somewhere and started applying this method ? or you have got some options and after some consideration you found it better to apply this method OR your requirement states that. Because normally me and my peers doesn't use bundles and directly pass the extras. And that works for me every time.
using this instead of bundle
i.putString("To", ConstantData.inbox_from);
Log.d("From Value", ConstantData.inbox_from);
i.putString("Subject", "RE:" + ConstantData.inbox_subject);
Log.d("Subject Value", ConstantData.inbox_subject);
i.putString("FromId", ConstantData.inbox_fromid);
Log.d("From Id Value",ConstantData.inbox_fromid);
and in another class..
to = getIntent().getString("To");

Categories

Resources