java - Fragment newInstance too much data saved to bundle - android.os.TransactionTooLargeException -


i have created fragment standard way (skeleton generated android studio). have newinstance static method:

public static eventsmapfragment newinstance(list<event> eventlist, string accesstoken, string tokentype, string cityid, boolean ismessage) {      bundle args = new bundle();      args.putstring("events", new gson().tojson(eventlist));      eventsmapfragment fragment = new eventsmapfragment();       ///...........      fragment.setarguments(args);      return fragment; } 

then in, oncreate override need retrieve collection, quite standard way:

 @override  public void oncreate(bundle savedinstancestate)  {     super.oncreate(savedinstancestate);     if (getarguments() != null)     {         string json = getarguments().getstring("events");         type type = new typetoken<list<event>>(){}.gettype();         meventlist = new gson().fromjson(json, type);    } 

the problem eventlist collection large, i'm getting exception:

java.lang.runtimeexception: android.os.transactiontoolargeexception: data parcel size 592196 bytes

somewhere else in app (weird, json passed correctly, system not allow pass more bundle, not ease track exact problem is).

because newinstance() method static, unable create retain fragment , pass data (i have no idea how in case).

my solution use weakreference pass data, that:

public class dataholder {     private static final dataholder holder = new dataholder();     map<string, weakreference<object>> data = new hashmap<>();      public static dataholder getinstance() {return holder;}      public void save(string id, object object)     {         data.put(id, new weakreference<>(object));     }      public object retrieve(string id)     {         weakreference<object> objectweakreference = data.get(id);         return objectweakreference.get();     } } 

then, store data (instead of using bundle):

dataholder.getinstance().save("eventlist", eventlist); 

and retrieve data:

meventlist = (list<event>) dataholder.getinstance().retrieve("eventlist"); 

however, not work on kitkat (it seems working fine on 5.1 , newer). main trouble unable debug on kitkat, because of multidex, debug compilation won't run on kitkat (release has minifyenabled=true , no multi dex). seems, on kitkat, this:

meventlist = (list<event>) dataholder.getinstance().retrieve("eventlist"); 

is null.

do have ideas? current solution check build version , pass json bundle on kitkat (it works on kitkat, problem 7.0 , newer) , use weakreference on newer android versions.

but i'm not happy this.

[edit] last try events collection parent activity. in main activity, have following code (one app drawer menu click):

if (menuid == r.id.nav_events_map) {   eventstodraw =  lists.newarraylist(collections2.filter(events,x->!utils.isgeojsonempty(x.getgeojson())));   //....................   if(eventstodraw.size() >0)   {     mfragment = eventsmapfragment.newinstance();     replacefragment(mfragment);   }   else   {    mfragment = nodatafragment.newinstance(getstring(r.string.message_no_events));    replacefragment(mfragment);  } } 

eventstodraw private field, have created getter:

public list<event> geteventstodraw() {   return eventstodraw; } 

then, inside map fragment:

@override  public view oncreateview(@nonnull layoutinflater inflater, viewgroup container, bundle savedinstancestate)  {   //map initialization stuff   //....................     meventlist = ((mainactivity)getactivity()).geteventstodraw();    //draw on map stuff etc  } 

the problem i'm getting meventlist null. after debugging, noticed, geteventstodraw() called early, won't return collection, returns null, because there no data available yet.

it works on kitkat

no, not. 1mb ipc transaction limit has existed many years. however, limit simultaneous ipc transactions process, , failures probabilistic. example, 592196 less 1mb, crashed may make through ok sometimes, because there happens little else going on ipc @ moment in time.

so have newinstance static method

don't pass collection of events. pass barest minimum information necessary fragment collection of events. use process-level pojo cache minimize redundant i/o, support hitting database/network/whatever again if needed, handle both configuration changes (process cache still around) , process termination (but user returns task quickly, instance stat still used).


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -