Passing more than one array using bundle - java

I am trying to pass 2 double arrays from one activity to the other. However when I try to pass the values from the 2 arrays in the first activity to the arrays in the second activity I get just the values from the first array and its stored in both new arrays.
This is how I use the bundle to send the arrays
Bundle bund = new Bundle();
bund.putDoubleArray(endLatitudeStr, endLatitude);
intent.putExtras(bund);
Bundle bund2 = new Bundle();
bund2.putDoubleArray(endLongitudeStr, endLongitude);
intent.putExtras(bund2);
startActivity(intent);
And the on the receiving side I have:
Intent intent = getIntent();
mXmlRpcUrl = intent.getStringExtra("XmlRpcUrl");
mSessionID = intent.getStringExtra("SessionID");
mGetSavedTripFunc = intent.getStringExtra("GetSavedTripFunc");
Bundle bund = intent.getExtras();
endLatitude = bund.getDoubleArray(endLatitudeStr);
Bundle bund2 = intent.getExtras();
endLongitude = bund2.getDoubleArray(endLongitudeStr);
However the result is always just the values from the first array(in this case endLatitude)
What am i doing wrong?

Use same bundle object.
Bundle bund = new Bundle();
bund.putDoubleArray(endLatitudeStr, endLatitude);
bund.putDoubleArray(endLongitudeStr, endLongitude);
intent.putExtras(bund);
startActivity(intent);
Intent intent = getIntent();
mXmlRpcUrl = intent.getStringExtra("XmlRpcUrl");
mSessionID = intent.getStringExtra("SessionID");
mGetSavedTripFunc = intent.getStringExtra("GetSavedTripFunc");
Bundle bund = intent.getExtras();
endLatitude = bund.getDoubleArray(endLatitudeStr);
endLongitude = bund.getDoubleArray(endLongitudeStr);

If i recall correctly you can only use one bundle because if you make another bundle it will replace the previous bundle so what you need to do is put the bund1 and bun2 on the first bundle then retrieve it
use
Bundle bundle = new Bundle();
bundle = getIntent().getExtras();
String mystring=bundle.getString("bund1");
String mystring=bundle.getString("bund2");

Why do you use 2 Bundles? Just use one ...
Bundle bundle = new Bundle();
bundle.putDoubleArray(endLatitudeStr, endLatitude);
bundle.putDoubleArray(endLongitudeStr, endLongitude);
intent.putExtra(bundle);
startActivity(intent);
and ...
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
endLatitude = bundle.getDoubleArray(endLatitudeStr);
endLongitude = bundle.getDoubleArray(endLongitudeStr);

Related

Is there a point in using Bundle class in android for passing info between activities?

This is my main activity:
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
String name = "James";
intent.putExtra("title", name);
MainActivity.this.startActivity(intent);
and this is the second activity:
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String name = bundle.getString("title");
tv.setText(name);
I know that I can take out the Bundle class and just get the string by intent object. So, in this case there is no need for Bundle class. So what is this class good for in this case? It seems to me it's just a redundant line of code.
In what cases using Bundle is advised for passing variables between classes?

Can't pass string value

I want to create a simple calulator, and I get value of second activity of 0 no matter what I write in text View.
Intent intent = new Intent(this, DisplayResultActivit.class );
EditText edittext = findViewById(R.id.liczba);
EditText edittext2 = findViewById(R.id.liczba2);
int wpis2 = Integer.valueOf(edittext.getText().toString());
int wpis = Integer.valueOf(edittext2.getText().toString());
Bundle extras = new Bundle();
extras.putInt("wpis", wpis);
extras.putInt("wpis2", wpis2);
startActivity(intent);
2 activity :
Intent intent = getIntent();
Bundle extras = intent.getExtras();
int wpis = 0;
if (extras != null) {
wpis = extras.getInt("wpis1");
}
int wpis2 = 0;
if (extras != null) {
wpis2 = extras.getInt("wpis2");
}
TextView tv = findViewById(R.id.result);
tv.setText(String.valueOf(wpis) + String.valueOf(wpis2));
As mention in the comment. You also need to link the Bundle variable with the Intent variable. See the follow link: https://zocada.com/using-intents-extras-pass-data-activities-android-beginners-guide/
//create a Bundle object
Bundle extras = new Bundle();
//Adding key value pairs to this bundle
//there are quite a lot data types you can store in a bundle
extras.putString("USER_NAME","jhon Doe");
extras.putInt("USER_ID", 21);
extras.putIntArray("USER_SELCTIONS", [1, 2, 3, 4, 5]);
...
//create and initialize an intent
Intent intent = new Intent(this, NextActivity.class);
//attach the bundle to the Intent object
intent.putExtras(extras);
//finally start the activity
startActivity(intent);
So your code needs to be:
Intent intent = new Intent(this, DisplayResultActivit.class );
EditText edittext = findViewById(R.id.liczba);
EditText edittext2 = findViewById(R.id.liczba2);
int wpis2 = Integer.valueOf(edittext.getText().toString());
int wpis = Integer.valueOf(edittext2.getText().toString());
Bundle extras = new Bundle();
extras.putInt("wpis", wpis);
extras.putInt("wpis2", wpis2);
intent.putExtras(extras);
startActivity(intent);
Just change your code to:
Intent intent = new Intent(this, DisplayResultActivit.class );
EditText edittext = findViewById(R.id.liczba);
EditText edittext2 = findViewById(R.id.liczba2);
int wpis2 = Integer.valueOf(edittext.getText().toString());
int wpis = Integer.valueOf(edittext2.getText().toString());
Bundle extras = new Bundle();
extras.putInt("wpis", wpis);
extras.putInt("wpis2", wpis2);
intent.putExtras(extras);
startActivity(intent);
You forgot about putExtras() method!

How to transfer all mappings from Bundle to another bundle without overwriting it?

I have tried this but it overwrites the existing bundle:
Bundle b1 = new Bundle();
b1.putString("name", "Abraham");
Intent i = getIntent();
Bundle b2 = i.getExtras();
b1.putAll(b2);
Than i lose abraham...
What you are doing here is :
//Creating a new Bundle
Bundle b1 = new Bundle();
//Putting some value in that bundle
b1.putString("name", "Abraham");
//Creating a new Intent
Intent i = getIntent();
and here without assigning b1(bundle) to your intent like this
i.putExtras(b1);
//here you are getting a null bundle from intent.
Bundle b2 = i.getExtras();
// So you didn't get any bundle here snd getting an exception
b1.putAll(b2);
You just have to assign your bundle to intent. like mention above in bold.

Android: Passing String Array over Multiple Activities

I am trying to pass a single string array through about 3 classes to finally have the contents of the array[1] printed to a textview. I've been using intent to achieve this with my arraylists and it works fine. For some reason I'm unable to get it working with a measly string array. Here's what I'm doing.
Origin Activity of String Array:
private String [] decisionInput = new String[1];
textData = etShouldI.getText().toString();
if (!textData.matches("")){
decisionInput[0] = (String.valueOf(textData));
test.setText(decisionInput[0]); //TEST WORKS
//CREATE BUNDLE
Bundle bundle = new Bundle();
bundle.putStringArray("decision", decisionInput);
//SEND BUNDLE DATA
Intent intent = new Intent(this,Pro.class);
intent.putExtras(bundle);
startActivity(intent);}
In my next Activity I've got the following, in order to receive the data, and send it off to the next Activity, and so on...
String[] dPasser = new String[1];
#ONCREATE
//BUNDLE RECEIVER
Bundle bundle = getIntent().getExtras();
dPasser = bundle.getStringArray("decision");
thisText.setText(String.valueOf(dPasser)); //TV currently returns null...
#ONCLICK
//SEND DECISION DATA TO NEXT ACTIVITY
Intent intent = new Intent(this, Next.class);
Bundle b = new Bundle();
b.putStringArray("decision", dPasser);
intent.putExtras(b);
startActivity(intent);
What the $%#& am I doing wrong guys?
You put the code below in a file named data, in your code you then use just it by calling data.array
public class data {
public String[] array = new String[1];
}
But going with just passing through a String[] you shouldn't need bundle.
simply
intent.putExtra("stringArray".String[]);
and get it with
this.getIntent().getStringArrayExtra("stringArray")

Java extras bundle is returning null values

My bundle is returning null strings for the contained extras. Not NPEs, actual "null" values. Any ideas on why this would be happening?
new bundle
String u = null;
Bundle b = new Bundle();
Intent i = new Intent(view.getContext(), ******.class);
u = api.companyData.link.get(position);
Log.d("URL++++++++++++++++++++", u);
b.putString("graphic", api.companyData.graphic);
b.putString("name", api.companyData.name);
b.putString("url", u);
i.putExtras(b);
startActivity(i);
The log statement is returning the url fine.
Receiver of bundle
Bundle extras = getIntent().getExtras();
if(extras !=null) {
Log.d("EXTRAS", extras.getString("name")+extras.getString("graphic")+extras.getString("link"));
D/EXTRAS ( 4698): nullnullnull
I always do it this way:
Intent i = new Intent(view.getContext(), ******.class);
i.putExtra("url", u);
and then
String url = getIntent().getStringExtra("url");
I've never tried it your way, but if you look at the Android docs for putExtras(Bundle), it says:
Add a set of extended data to the intent. The keys must include a
package prefix, for example the app com.android.contacts would use
names like "com.android.contacts.ShowAll".
That you're not doing that may be the reason for the failure.
put the objects in the intent directly, e.g.
Intent i = new Intent(view.getContext(), ******.class);
i.putExtra("graphic", ...);
i.putExtra("name", ...);
i.putExtra("url", ....);
Then in the receiver activity:
getIntent().getStringExtra("graphic");
getIntent().getStringExtra("name");
getIntent().getStringExtra("url");

Categories

Resources