0

I get the following error when I put my app in the background, or when I press the recent apps button while in the app:

java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.example.mappractice.data.LocationItem)

This is the class which generates the error(this is used as an argument for a fragment):

data class LocationItem(
    val name: String,
    val locations: List<Place>
): Serializable

This is where the argument is used:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.mapView.onCreate(savedInstanceState)

        val location = args.location

        binding.mapView.getMapAsync(object : OnMapReadyCallback {
            override fun onMapReady(googleMap: GoogleMap) {
                val boundsBuilder = LatLngBounds.builder()

                for (place in location.locations) {
                    val locationPoz = LatLng(place.lat, place.long)
                    boundsBuilder.include(locationPoz)

                    val marker = MarkerOptions()
                        .position(locationPoz)
                        .title(place.name)

                    googleMap.addMarker(marker)
                }

                googleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(
                    boundsBuilder.build(),
                    800,
                    800,
                    0))
            }
        })
    }
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Dragos Ciupe
  • 109
  • 1
  • 7
  • What if you used a Parcelable class instead `@Parcelize data class LocationItem( val name: String, val locations: List ): Parcelable` ? – Zain Dec 01 '21 at 17:17
  • What does parcelable means – Dragos Ciupe Dec 01 '21 at 19:16
  • It's the android right implementation of Serializable.. but it's greater performance.. check out [this](https://stackoverflow.com/questions/49249234/what-is-parcelable-in-android) – Zain Dec 01 '21 at 19:18

1 Answers1

0

I had a similar problem. Even using @Parcelize and Parcelable, the app crashes when going background or starting another Activity. Log showed "java.lang.StackOverflowError: stack size 8MB". The problem was that I had a duplicate attribute, for example:

@ Parcelize
data class League(
     val matches: List<Match>?,
...
) : Parcelable

and

@ Parcelize
data class Match(
     val league: League?,
...
) : Parcelable

It was in loop. When I changed the attribute by creating another model "val league: LeagueInfo?" the problem was solved.