Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8497981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:16:43+00:00 2026-06-11T00:16:43+00:00

A behaviour i’m observing w.r.t passing serializable data as intent extra is quite strange,

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T00:16:44+00:00Added an answer on June 11, 2026 at 12:16 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just see strange behaviour in Eclipse . I am developing one android project
Strange behaviour. I use rich:editor with these attributes: (Irrelevant data removed) HtmlEditor editor =
I get some strange behaviour when I load data via my custom VirtualPathProvider. It
There's a strange behaviour that is drive me crazy. I've a table of users
There is a strange behaviour with json_encode and json_decode and I can't find a
I'm seeing strange behaviour when using PostgreSQL in a Hibernate/JPA environment with a single
The behaviour of the delete operator seems very complicated and there are many misunderstandings
Wierd behaviour when passing values to and from second form. ParameterForm pf = new
I am getting strange behaviour when trying to upload files, using jQuery multi-file plugin,
i have strange behaviour of git - push is working, but clone is not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.