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.
Related
The are different ways how to pass data among components and apps in Android. For instance, here are some of them:
Intent intent = new Intent(this, DestinationActivity.class);
intent.putExtra("key", "value");
or
Bundle args = new Bundle();
args.putInt("someInt", someInt);
args.putParcelable("key", ParcelableObject);
Intent intent = new Intent();
intent.setAction("someAction");
intent.putExtra("key", arg);
or
fragment.setArguments(args);
As I know in java primitive values are stored in a stack and objects are placed in a heap. So, I would like to know what's happening when we call these methods: bundle.putInt(int) and bundle.putParcelable(Object), intent.putExtra("key", "string value") and fragment.setArguments(args) in Android.
Not all that much actually happens. Inside of Bundle is a Map. putInt will convert the integer primitive to an Integer object, and put the Integer into the map. putString will put the String object there. putParcelable will put an object that extends Parcelable into the map. That's all that happens at that time.
When startActivity is called, it will walk that map, and basically build a stream of data. The format isn't JSON but it serves a similar purpose- its a well understood format that can be parsed to values later on. As it walks that map, it knows how to add primitives (int, double, etc) to that file. It also knows how to do Strings. For Parcelable objects, there's a function in the object that adds the object to the stream and one to parse it out of the stream. It then takes that stream and asks the OS to pass that stream to the process that implements the intent. THe Android framework in that application will parse the stream back into a map (creating new objects) and then pass it to onCreate.
Why do all this work? Because the intent you run may not be in your process. So it can't share them directly, it needs to make copies. The extras is just a built in serialization method, making it easy to pass complex data.
Why do we sometimes use Bundle and sometimes Intent? Well, every Intent has a Bundle inside it. Calling intent.putIntExtra will call Bundle.putInt on the bundle inside that intent. Its just a convenience method so you don't need to call intent.getExtras().putInt().
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.
A behaviour i'm observing w.r.t passing serializable data as intent extra is quite strange, and I just wanted to clarify whether there's something I'm not missing out on.
So the thing I was trying to do is that in ActivtyA I put a LinkedList instance into the intent I created for starting the next activity - ActivityB.
LinkedList<Item> items = (some operation);
Intent intent = new Intent(this, ActivityB.class);
intent.putExtra(AppConstants.KEY_ITEMS, items);
In the onCreate of ActivityB, I tried to retrieve the LinkedList extra as follows -
LinkedList<Item> items = (LinkedList<Item>) getIntent()
.getSerializableExtra(AppConstants.KEY_ITEMS);
On running this, I repeatedly got a ClassCastException in ActivityB, at the line above. Basically, the exception said that I was receiving an ArrayList. Once I changed the code above to receive an ArrayList instead, everything worked just fine.
Now I can't just figure out from the existing documentation whether this is the expected behaviour on Android when passing serializable List implementations. Or perhaps, there's something fundamentally wrong w/ what I'm doing.
Thanks.
I can tell you why this is happening, but you aren't going to like it ;-)
First a bit of background information:
Extras in an Intent are basically an Android Bundle which is basically a HashMap of key/value pairs. So when you do something like
intent.putExtra(AppConstants.KEY_ITEMS, items);
Android creates a new Bundle for the extras and adds a map entry to the Bundle where the key is AppConstants.KEY_ITEMS and the value is items (which is your LinkedList object).
This is all fine and good, and if you were to look at the extras bundle after your code executes you will find that it contains a LinkedList. Now comes the interesting part...
When you call startActivity() with the extras-containing Intent, Android needs to convert the extras from a map of key/value pairs into a byte stream. Basically it needs to serialize the Bundle. It needs to do that because it may start the activity in another process and in order to do that it needs to serialize/deserialize the objects in the Bundle so that it can recreate them in the new process. It also needs to do this because Android saves the contents of the Intent in some system tables so that it can regenerate the Intent if it needs to later.
In order to serialize the Bundle into a byte stream, it goes through the map in the bundle and gets each key/value pair. Then it takes each "value" (which is some kind of object) and tries to determine what kind of object it is so that it can serialize it in the most efficient way. To do this, it checks the object type against a list of known object types. The list of "known object types" contains things like Integer, Long, String, Map, Bundle and unfortunately also List. So if the object is a List (of which there are many different kinds, including LinkedList) it serializes it and marks it as an object of type List.
When the Bundle is deserialized, ie: when you do this:
LinkedList<Item> items = (LinkedList<Item>)
getIntent().getSerializableExtra(AppConstants.KEY_ITEMS);
it produces an ArrayList for all objects in the Bundle of type List.
There isn't really anything you can do to change this behaviour of Android. At least now you know why it does this.
Just so that you know: I actually wrote a small test program to verify this behaviour and I have looked at the source code for Parcel.writeValue(Object v) which is the method that gets called from Bundle when it converts the map into a byte stream.
Important Note: Since List is an interface this means that any class that implements List that you put into a Bundle will come out as an ArrayList.
It is also interesting that Map is also in the list of "known object types" which means that no matter what kind of Map object you put into a Bundle (for example TreeMap, SortedMap, or any class that implements the Map interface), you will always get a HashMap out of it.
The answer by #David Wasser is right on in terms of diagnosing the problem. This post is to share how I handled it.
The problem with any List object coming out as an ArrayList isn't horrible, because you can always do something like
LinkedList<String> items = new LinkedList<>(
(List<String>) intent.getSerializableExtra(KEY));
which will add all the elements of the deserialized list to a new LinkedList.
The problem is much worse when it comes to Map, because you may have tried to serialize a LinkedHashMap and have now lost the element ordering.
Fortunately, there's a (relatively) painless way around this: define your own serializable wrapper class. You can do it for specific types or do it generically:
public class Wrapper <T extends Serializable> implements Serializable {
private T wrapped;
public Wrapper(T wrapped) {
this.wrapped = wrapped;
}
public T get() {
return wrapped;
}
}
Then you can use this to hide your List, Map, or other data type from Android's type checking:
intent.putExtra(KEY, new Wrapper<>(items));
and later:
items = ((Wrapper<LinkedList<String>>) intent.getSerializableExtra(KEY)).get();
If you are using IcePick library and are having this problem you can use Ted Hoop's technique with a custom bundler to avoid having to deal with Wrapper instances in your code.
public class LinkedHashmapBundler implements Bundler<LinkedHashMap> {
#Override
public void put(String s, LinkedHashMap val, Bundle bundle) {
bundle.putSerializable(s, new Wrapper<>(val));
}
#SuppressWarnings("unchecked")
#Override
public LinkedHashMap get(String s, Bundle bundle) {
return ((Wrapper<LinkedHashMap>) bundle.getSerializable(s)).get();
}
}
// Use it like this
#State(LinkedHashmapBundler.class) LinkedHasMap map
To send intent with any object from one activity to another activity we can use Parcelable Interface or Serializable Interface. What is the difference between two? Which one is preferable to use?
Moreover, we can send string, integer type of object without using all this Interface. How it is possible?
Java Serializable:
Serializable comes from standard Java and is much easier to implement all you need to do is implement the Serializable interface and add override two methods.
private void writeObject(java.io.ObjectOutputStream out)
throws IOException
private void readObject(java.io.ObjectInputStream in)
throws IOException, ClassNotFoundException
The problem with Serializable is that it tries to appropriately handle everything under the sun and uses a lot reflection to make determine the types that are being serialized. So it becomes a beefy Object
Androids Parcelable:
Android Inter-Process Communication (AIPC) file to tell Android how is should marshal and unmarshal your object.It is less generic and doesn't use reflection so it should have much less overhead and be a lot faster.
You can send String, Integer and such data types, and also the objects of the classes that implemented Parcelable interface as follows...
Intent intent = new Intent(CallingActivity.this, CalledActivity.class);
intent.putExtra("IntegerExtra", intValue);
intent.putExtra("StringExtra", stringValue);
intent.putExtra("ParcExtra", parcObject);
startActivity(intent);
And, at the receiving end you can write the following code,
intValue = getIntent().getIntExtra("IntegerExtra", 0);
stringValue = getIntent().getStringExtra("StringExtra");
parcObject = ((YourParcalabeDataType) getIntent().getParcelableExtra("ParcExtra"));
Hope this may solve your problem...
:)
Intead of sending an object, it may be easier to just send a URI that points to your content. This would simplify the sending and would remove the need to send the object, since the URI would ideally point to the content you are interested in. Of course this depends on the content that you're trying to pass.
You can find difference between Parcelable and Serializable Interface from that link .
Basically Parcelable is created for android and is far more efficient than Serializable.
You can simply send string or integer by using Bundles and linking those bundles to intents.
Intent i = new Intent(getApplicationContext(),YourClass.class);
Bundle b = new Bundle();
b.putString("string", "string");
i.putExtras(b);
startActivity(i);
So I've always been under the assumption that you can't pass arrays between activities with extras. But I was just going through the Bundle class docs and saw putStringArray(String key, String[] value) and public String[] getStringArray (String key). Are these new? They both say since API lvl 1, but I don't remember being able to pass arrays. Am I going crazy here?
I think you must be able to pass string[] as a bundle in Android. Any specific reason you have for getting into such a conclusion?
Refer to this post
http://www.anddev.org/view-layout-resource-problems-f27/how-can-i-pass-multidimensional-string-array-two-activities-t9259.html
You can pass any Serializable object as extra, so I don't see why you could not pass String array. There is a setExtra(String, Serializable) method in the Intent, that's what I use.