androidkotlinarraylistandroid-activity

How to Pass ArrayList of Custom Objects Between Activities in Kotlin on Android Studio


I'm working on an Android project in Kotlin where I store user information in a custom object. I need to pass an ArrayList<CustomObject> from one activity to another and display the stored information in a list format.

I've tried reading various threads, but I encountered issues with serialization being less efficient than Parcelable. However, some methods like getParcelableArrayListExtra are deprecated.

What's the best way to pass an ArrayList<CustomObject> between activities in 2024?

Note: I understand this is a theoretical question, so I'm not including code. I'm looking for a current, efficient solution for passing data between activities.


Solution

  • you can use

    val customObjectList = arguments?.getParcelableArray("customObjectList")?.toList() as? List<CustomObject>
    

    This approach ensures type safety and reduces the risk of runtime errors. It's also more efficient than using serialization.