Passing primitive types to a Bundle - android

When i use Intent.putExtra to a Bundle to pass a int value, is that primitive bumped up to an Object first. I though only Parceable or Serializable objects could be passed to a Bundle. How about primitives. Is this bumped up to an Integer ?
Kind Regards.

Yes. Internally Bundle uses HashMap<String, Object>();
see here
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2.1_r1/android/os/Bundle.java#Bundle.0mMap

You can pass primitives types of variables with the intent or Bundle without making it parcelable or serializable.
When you will have to pass your custom object then you require to make it parcelable or Serializable. Parcelable is the best choice for Android.

Related

How to pass a CopyOnWriteArrayList to a Bundle object?

I have a data structure MyObject that implements the Parcelable interface, and I want to pass a CopyOnWriteArrayList<MyObject> object to a Bundle object to create a new Fragment. So I tried
Bundle args = new Bundle();
args.putParcelableArrayList("RssItems", rssItems);
But since CopyOnWriteArrayList is not a subclass of ArrayList, it does not match the method signature.
Is there any way to pass a CopyOnWriteArrayList to a Bundle object?
Think about it this way:
CopyOnWriteArrayList is simply a very special implementation of a list. You don't actually care about Parceling the implementation, you just care about Parceling up the contents of the list.
If you look at the Parcelable interface, you'll see that that is dead simple. In fact, you can probably copy the code (from AOSP) that parcels an ArrayList, directly.
While it is true that you can implement Serializable, it is no easier than implementing Parcelable and it is way slower.
you can use the Serializable instead to write the object to the bundle.
sample:
You need to just implement Serializable in your object
public class CopyOnWriteArrayList implements Serializable
Putting and getting it in the Bundle:
args.putSerializable(key, value)
args.getSerializable(key)

Pass a custom object list to an activity: parcelable or serializable?

From android documentation:
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 Parcelable approach requires that you explicitly serialize the members of your class, but in the end, you get a much faster serialization of your objects.
So we know that Parcelable is actually better than Serializable but, on the other hand,
Also realize that Android provides two mechanisms that allow you to pass data to another process. The first is to pass a bundle to an activity using an intent, and the second is to pass a Parcelable to a service. These two mechanisms are not interchangeable and should not be confused. That is, the Parcelable is not meant to be passed to an activity. If you want to start an activity and pass it some data, use a bundle. Parcelable is meant to be used only as part of an AIDL definition.
Ok man, but I need to pass a custom object list to my activity!So into the bundle I still have to put either a parcelable or a serializable!
For now I did this way:
public class MyObject implements Serializable{
and to pass:
Bundle b = new Bundle();
b.putSerializable("objList", anArrayListWithMyObjectElements);
intent.putExtra("objList", b);
as ArrayList implements Serializable too it works fine...but I don't see the point of using a bundle this way...but android tells me to not use Parcelable for activity communication...what's the correct answer??
In my exprience, I strongly suggest using Parcelable when you can, IPC or not. It's Android's de facto replacement for Java's Serializable, and it's more optimized.
If you need help on how to parcel an object, let me know.

How to extract HashMap from Bundle without cast?

Communicating between two threads, I use a Message, transporting the data. In my case, a HashMap. Now on reconstructing the data, I get a warning about an unchecked type cast. That would imply to me (as java noob), that I shouldn't do that cast at all, right? But what would be the correct way to get the HashMap out of the bundle?
Bundle dataBundle = msg.getData();
Serializable result = dataBundle.getSerializable("data");
HashMap<String,String> output = (HashMap<String, String>) result;
Thanx for any pointers!
Marcus
So with your approach
getSerializable("data");
there is no way to do it without casting because getSerializable method always returns Serializable instance.
Without casting you could use getParcelableExtra that returns T but i know anything about your application context so i'm not sure if it's possible to use.

Definition of Android Bundle

I am new to Android. Can you tell me what is a Bundle and how they are used in android?
Bundle generally use for passing data between various Activities. It depends on you what type of values you want to pass but bundle can hold all types of values and pass to the new activity.
You can use it like ...
Intent intent = new
Intent(getApplicationContext(),SecondActivity.class);
intent.putExtra("myKey",AnyValue);
startActivity(intent);
Now you can get the passed values by...
Bundle extras = intent.getExtras();
String tmp = extras.getString("myKey");
you can also find more info on android-using-bundle-for-sharing-variables and Passing-Bundles-Around-Activities
Copy from Here.
Read this:
http://developer.android.com/reference/android/os/Bundle.html
It can be used to pass data between different Activity's
Android using Bundle for sharing variables. Bundle is used to pass data between Activities. You can create a bundle, pass it to Intent that starts the activity which then can be used from the destination activity.
Here is Good Sample Example.
The Android developer reference overview states:
A mapping from String values to various Parcelable types.
IOW, a Bundle is a set of key/value pairs, where it implements an interface called Parcelable.
Unlike a C++ map, which is also a container of key/value pairs but all values are of the same type, a Bundle can contain values of different types.

What needs to be explicity bundled

When bundling an object for later retrieval do I have to bundle objects within those objects?
For example, if I have an object that represents a player in a card game and within that I instantiate an object that represents the player's hand, do I have save the inner object to the bundle or is that automatically included with the outer one?
You cannot bundle any old Object, it has to be a String or a primitive such as boolean, integer, 'byte' or an array of these simple things. In this case yes, the contents of a String[] array are saved with the Bundle.
For more complex structures you can use implement the Parcelable in your object class, but it will be up to you to make sure the object saves all necessary information to it's Parcel and restores it.
java.ui.Serializable is something worth checking. It pretty much automates bundling class and its member variables as long as your class and all required members implement Serializable interface too.
http://www.tutorialspoint.com/java/java_serialization.htm

Categories

Resources