How to pass a CopyOnWriteArrayList to a Bundle object? - android

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)

Related

How pass object to new activity if object not implemet Serializable or Parceble?

I want pass my object to new Activity by intent. But I can not change my objet and implement it from Serializable or Parceble. How can i pass this object whith another way?
You can use JSON for this purpose.
First of all you have to create a POJO class and then do the following in your FirstActivity. Object is your POJO class.
Object obj = new Object();
String toJson = new Gson().toJson(obj);
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("json", toJson);
startActivity(intent);
Now do the following in your SecondActivity.
String stringExtra = getIntent().getStringExtra("json");
Object fromJson = new Gson().fromJson(stringExtra, Object.class);
Now you can access your setters and getters of your POJO class using "fromJson".
The easiest option would be gson!
Although I should mention that gson is a bit slower than either Parcelable or Seralizable. If your classes are simple enough, then it might be worth wrapping them in your own parcelable class.
Can you recreate it manually? I.e. call its constructor and its setters. If yes, you should either create a Parcelable as wrapper for the object.
You could also write a POJO representing the data required for rebuilding the object on the activity and make that class Parceble or Serializable then pass that pojo and make a method that build an object of your type from the pojo
Without using parceble or serializable you cannot pass java objects. Thats it. You can make an instance of hash map and store values in a key-value pair and then put it in a bundle and pass it in an intent to another activity, but this is NOT the modular way to do it.

Need for Parcelable in android

I want to know when to use parcelable and when not to . I know that parcelable is way of parcel complex data type in android , but as per official document http://developer.android.com/guide/faq/framework.html#3 , nothing such is mention . So when is parcelable really needed ??
By implementing the Parcelable interface, you can make your class object capable of being stored in a Bundle and you can then easily pass it to another activity with the help of an Intent.
e.g.
Intent i = new Intent(....);
i.putParcelableExtra("name", object);
Then in the other activity, get it like this:
YourClass object = (YourClass) getIntent().getExtras().getParcelableExtra("name");
Notice the typecasting. That is necessary in order to use your class's methods.
To pass data to another activity, we are usually put bundle object on activity intent that will be called. Bundle can be filled with primitive data types like long, integer, and boolean. It can be filled with simple data type like String class to represent text. For example, an activity calls another activity at the same time sends simple data to it.
In destination activity, we check the bundle. If it is exist, we open the data of bundle from the origin activity.
Now, how if we want to pass the complex data type like our defined class object to another activity? For this need, we can use Parcelable in Android.
Parcelable is an interface for classes so a class that implements Parcelable can be written to and read from a Parcel. The data in Parcel form can be passed between two threads. Parcel itself is a class that have abilities to serialize and deserialize object of class.

Passing primitive types to a Bundle

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.

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.

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