0

Is there any workaround how to transfer larger amount of JSON files into another Activity?

I'm getting android.os.TransactionTooLargeException: data parcel size 640176 bytes

Activity is taking payload of certain array of JSONObjects which has to be used in that Activity. But payload size is dynamic. Sometimes its 20 objects, sometimes 2, but currently in this case its 364 objects inside JSONArray and I cant pass it to Activity.

fun openStationViewer(stations: List<CityStation>){
        val int = Intent(this, CityStationMapActivity::class.java)
        val bundle = Bundle()
        val stationsJs = CityStationLoader.convertStationsToPayload(stations)
        bundle.putString(CityStationMapActivity.CITY_STATIONS, stationsJs.toString())
        int.putExtras(bundle)
        startActivityForResult(int, REQUEST_CITY_STATION_MAP)
}
martin1337
  • 2,384
  • 6
  • 38
  • 85
  • Perhaps you should not have two activities. Instead, have one activity with two fragments or composables representing the screens. – CommonsWare May 25 '23 at 10:18

2 Answers2

0

You should not pass the actual data between screens as you may run into the TransactionTooLargeException, which is also kinda tricky as the transaction that blows up is not necessarily the one causing issues as the size is determined by the sum of transactions leading up to the crash.

Using a statically accessed 'object' is in my opinon bad practice and a sign that the architecture is lacking.

Instead:

  • Store the data in a shared Repository or similar abstraction
  • Pass an id to the data in the transaction between screens
  • Lookup the data in the repository using the id on the other side

Sharing the repository can be done in different ways but doing it with DI using Dagger/Hilt or Koin is a good way to go.

patrick.elmquist
  • 2,113
  • 2
  • 21
  • 35
-1

You can create a singleton object and pass it through the relevant variable. There is a 1mb limit in the bundle.

object Singleton {
   var singletonProp : String? = null
}

Singleton.singletonProp =syourJson
Arda Kazancı
  • 8,341
  • 4
  • 28
  • 50