Sending arrays with Intent.putExtra
You are setting the extra with an array. You are then trying to get a single int. Your code should be: int[] arrayB = extras.getIntArray(“numbers”);
You are setting the extra with an array. You are then trying to get a single int. Your code should be: int[] arrayB = extras.getIntArray(“numbers”);
Bundles are generally used for passing data between various Android activities. It depends on you what type of values you want to pass, but bundles can hold all types of values and pass them to the new activity. You can use it like this: Intent intent = new… Intent(getApplicationContext(), SecondActivity.class); intent.putExtra(“myKey”, AnyValue); startActivity(intent); You can … Read more
From “Pro Android 2” NOTE: Seeing Parcelable might have triggered the question, why is Android not using the built-in Java serialization mechanism? It turns out that the Android team came to the conclusion that the serialization in Java is far too slow to satisfy Android’s interprocess-communication requirements. So the team built the Parcelable solution. The … Read more
You can pass an ArrayList<E> the same way, if the E type is Serializable. You would call the putExtra (String name, Serializable value) of Intent to store, and getSerializableExtra (String name) for retrieval. Example: ArrayList<String> myList = new ArrayList<String>(); intent.putExtra(“mylist”, myList); In the other Activity: ArrayList<String> myList = (ArrayList<String>) getIntent().getSerializableExtra(“mylist”);
I would highly recommend a different approach. It’s possible if you REALLY want to do it, but it costs a lot of memory and is also slow. It might not work if you have an older phone and a big bitmap. You could just pass it as an extra, for example intent.putExtra(“data”, bitmap). A Bitmap … Read more
You have a few options: 1) Use the Bundle from the Intent: Intent mIntent = new Intent(this, Example.class); Bundle extras = mIntent.getExtras(); extras.putString(key, value); 2) Create a new Bundle Intent mIntent = new Intent(this, Example.class); Bundle mBundle = new Bundle(); mBundle.putString(key, value); mIntent.putExtras(mBundle); 3) Use the putExtra() shortcut method of the Intent Intent mIntent = … Read more