0

I think this is due to the large data transfer to Intent. How can I transfer Serializable data to another Activity without restrictions ?

Start to produce an error only in partitions with a large amount of data.

val stocksActivityIntent = Intent(context, StocksActivity::class.java)

stocksActivityIntent.putExtra("PARTNER_KEY", partners[position])
stocksActivityIntent.putExtra("CATEGORY_KEY", "StocksCategoty")

context.startActivity(stocksActivityIntent)
max taldykin
  • 12,459
  • 5
  • 45
  • 64
xew
  • 37
  • 6
  • 1
    Try check the logcat and post it to the question – nfl-x Aug 22 '19 at 02:45
  • 2019-08-22 13:30:20.412 1964-2285/system_process E/ActivityManager: Transaction too large, intent: Intent { cmp=ru757571.discount/.controllers.stocks.StocksActivity (has extras) }, extras size: 541596, icicle size: 0 2019-08-22 13:30:20.417 1964-2285/system_process E/JavaBinder: !!! FAILED BINDER TRANSACTION !!! (parcel size = 545232) – xew Aug 22 '19 at 04:31
  • It says `Transaction too large`, `extras size: 541596`. which means that your serialized class is too large to be passed through intent. I was unable to find documented limit, but tests show it is about 504 KB – Vladyslav Matviienko Aug 22 '19 at 04:44
  • I'm new to kotlin. How can I transfer data in another way ? – xew Aug 22 '19 at 04:47
  • what is that large in your class? – Vladyslav Matviienko Aug 22 '19 at 04:48
  • I am passing a json that is downloaded from the site – xew Aug 22 '19 at 04:50
  • you have to pass it some other way. For example save it to the file in one activity, and load in another – Vladyslav Matviienko Aug 22 '19 at 05:05
  • the limit is 1Mb and on api 27 and above i believe it will actually cause a crash – Kushan Aug 22 '19 at 05:30
  • the 1mb does not necessarily have to be just the data u passed btw, it will include all kinds of stuff like states, other system data etc and the bundle limit is for across all your application classes – Kushan Aug 22 '19 at 05:31
  • @Kushan how can I bypass this limit and transfer data ? – xew Aug 22 '19 at 05:34
  • @Vladyslav Matviienko It seems to me that writing to the file will be too slow, maybe there is a faster solution ? – xew Aug 22 '19 at 05:39
  • you can use some static field for example. – Vladyslav Matviienko Aug 22 '19 at 05:44
  • @Vladyslav Matviienko You can give a small example. I do so where you need to get var partner: Partner? = null. val activity = StocksActivity() activity.partner= partners[position] val stocksActivityIntent = Intent(context, activity::class.java) stocksActivityIntent.putExtra("CATEGORY_KEY", "StocksCategoty") context.startActivity(stocksActivityIntent) – xew Aug 22 '19 at 06:16
  • `StocksActivity()` never create an instance of Activity. – Vladyslav Matviienko Aug 22 '19 at 06:31
  • Check [this](https://stackoverflow.com/questions/11451393/what-to-do-on-transactiontoolargeexception) – nfl-x Aug 22 '19 at 07:46
  • You can try use this library: https://github.com/livefront/bridge or store data inside Singleton and retrieving that data inside other activity – cobe_tech Aug 22 '19 at 08:54

2 Answers2

0

StocksActivity.kt

companion object {
    var partner: Partner? = null
}

StocksCategory.kt

val stocksActivityIntent = Intent(context, StocksActivity::class.java)

StocksActivity.partner = partners[position]
stocksActivityIntent.putExtra("CATEGORY_KEY", "StocksCategoty")

context.startActivity(stocksActivityIntent)

@Vladyslav Matviienko, Thanks for your help )

xew
  • 37
  • 6
-1

Try

Bundle bundle = new Bundle();         
bundle.putSerializable("newItem", item); 
intent.putExtras(bundle);

In Activity class include

Intent intent = this.getIntent(); 
Bundle bundle = intent.getExtras();
item= (Item)bundle.getSerializable("newItem");
Pradeep
  • 261
  • 2
  • 7