Passing ArrayList between intents loses data

前端 未结 3 1855
小蘑菇
小蘑菇 2021-01-27 13:11

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

相关标签:
3条回答
  • 2021-01-27 13:58

    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 讨论(0)
  • 2021-01-27 14:04

    Try retrieving the array with

    getIntent().getExtras().getParcelableArrayList(yourArray);
    
    0 讨论(0)
  • 2021-01-27 14:05

    try this my friend:

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

    hope this helps

    0 讨论(0)
提交回复
热议问题