java - When does Android serialize objects? -


i working on android project, , want pass custom class mainactivitymodel fragment, mainactivityplaceholderfragment.

i have made mainactivitymodel serializable:

public class mainactivitymodel implements serializable{      public int current = 0;     public int pagecount = 0;      public boolean pristine = true;      // stores fetched datamap     public arraylist<hashmap<string, string>> arraylist;      public mainactivitymodel() {         this.arraylist = new arraylist<>();     }      public string getcategory() {         return util.categories[current];     }      public charsequence getmtitle () {         return  util.totitlecase(                 util.mapcategorypretty(util.categories[current]));     } } 

and passing fragment this:

public static mainactivityplaceholderfragment newinstance(mainactivitymodel mainactivitymodel) {     mainactivityplaceholderfragment fragment = new mainactivityplaceholderfragment();     bundle args = new bundle();     args.putserializable(arg_data_model, mainactivitymodel);     fragment.setarguments(args);     log.v(log_tag, "created: " + mainactivitymodel.getmtitle());     return fragment; } 

i access this:

@override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     mainactivitymodel = (mainactivitymodel) getarguments().getserializable(arg_data_model);     mmainactivityplaceholderfragmentview = new mainactivityplaceholderfragmentview(this, mainactivitymodel);      mcallbacks.onplaceholderfragmentcreated(mainactivitymodel.current); } 

i thought (after reading answers mention below), that serialization converts data bytes , restores them subsequently when needed. object copied. ok if want access data. wanted modify actual model (which referenced in mainactivity) fragment.

to experiment, set pristine false in fragment, , logging in mainactivity, indeed false.

but if serializable pass-by-value, how happening?

what i've read:

  1. what object serialization?
  2. r: pass reference
  3. what serializable in java
  4. what serialization in java?

a reference serializable object still object reference, it's no different passing list object or foo object. confusing part if , where serialization takes place.

from documentation of android.app.fragment.setarguments(bundle):

the arguments supplied here retained across fragment destroy , creation.

there 2 ways achieve this:

  • make bundle store raw bytes, , serialize/deserialize every get/put operation.
  • allow bundle hold live objects, , ask serialize/deserialize when fragment needs destroyed/recreated.

clearly, first option very inefficient: get/put operations more frequent activity/fragment life cycle changes. therefore, android serialize/deserialize when needed on life cycle changes.

this causes "weird" behavior in use case. assumed serializable object serialized immediately bundle, instead bundle holds reference object. since fragment not destroyed between newinstance , oncreate call, seeing exact same bundle holding exact same references.

of course, should not rely on these references stay intact. time application asked persist state (e.g. when going background, when rotating screen, or when system needs free ram), objects serialized , references gone. objects re-created serialized data, have different references.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -