开发者

Passing ArrayList between intents loses data

I am passing ArrayList<Custom implements Parcelable> myList to an Intent. Both of the following ways seem to work fine with putting the ArrayList into the new Intent.

        results.putParcelableArrayListExtra("list", myList);
        results.putExtra("list", myList);

When I check mIntent/mExtras/mMap/table it is everything there. But in the onCreate method of the intent some of that data seems to be lost. I am getting the ArrayList then with myList = (ArrayList<Custom>) this.getIntent().getParcelableArrayListExtra("list");

For example the list contains five items[a], [b], [c], [d] and [e]:

put...   get...
[a]  ->  [a]
[b]  ->  null
[c]  ->  [b]
[d]  ->  null
[e]  ->  [c]

It seems that every second item in the new list is not intended and tak开发者_StackOverflowes up one place that is missing at the end. Can you tell me what I am doing wrong?


try this my friend:

intent.putParcelableArrayListExtra("tag",yourObjectImplementsParceable);

xxx = (ArrayList<yourClassImplementsParceable>) intent.getParcelableArrayListExtra("tag");

hope this helps


Try retrieving the array with

getIntent().getExtras().getParcelableArrayList(yourArray);


Instead of writing the Parcelable implementation yourself, try to use the Android Studio code completion (in Windows: Alt+Enter once you write "implements Parcelable" and again on the CustomObject class name). This prevent common bugs in the implementation.

Even so, beware: if you implement Parcelable and add/remove object variables in the CustomObject afterwards, the Parcelable implementation won't be valid any more.

In this case, you can manually add the missing details to the Parcelable methods, but I prefer to delete them and make Android Studio implement them again (be sure to delete ALL of the implemented Parcelable methods in the CustomObject, since Android Studio won't update Parcelable methods that already exist).

I suffered the same bug that you wrote about, and, after correctly re-implementing Parcelable on the CustomObject (I had changed it a few times since I last implemented Parcelable), everything worked fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜