initialization - Android Fragment, static variables not initialized on "new"? -


i keep getting old data in fragment want have built scratch. it's driving me nuts ;)

here relevant code bits: in activity, when fragment created:

        ...         log.w(log_tag, "remotefrag not available");          // create fragment         int savepos=mdatamanager.getposition();         mdatamanager.setposition(position);         bundle args = new bundle();         remotefrag = new remotefragment();         args.putserializable(extraxmlparser, mdatamanager);         log.w(log_tag, "mdatamanager " + ((mdatamanager == null)? "not ":"") + "set");         remotefrag.setarguments(args);         fragmenttransaction transaction = getfragmentmanager().begintransaction();          transaction.replace(r.id.fragment_container, remotefrag);         if(mdatamanager.getposition()!=savepos)             transaction.addtobackstack(null);          // commit transaction         transaction.commit();         ... 

what want achieve: in activity, want relaunch (basically, app behaves different if in local wifi network, menu different , different options available. splash screen probes type of environment , initializes datamanager appropriately).

        ...         log.w(log_tag,"terminating...");         leaveremoteview();         fragmenttransaction transaction = getfragmentmanager().begintransaction();         transaction.remove(remotefrag);         remotefrag=null;         mdatamanager=null;         intent intent = getbasecontext().getpackagemanager().getlaunchintentforpackage(getbasecontext().getpackagename() );         intent.addflags(intent.flag_activity_clear_top);         startactivity(intent);         finish();         ... 

in remotefrag have:

public class remotefragment extends fragment implements datamanager.processxmldata  { remotedetail remotebottom = null; private view thisview; private viewgroup thiscontainer; static private datamanager _datamanager = null; static private boolean _datamanagerregistered = false;  final private static string log_tag = "remotefragment";   @override public view oncreateview(layoutinflater inflater, viewgroup container,          bundle savedinstancestate) {      // if activity recreated (such screen rotate), restore     // previous article selection set onsaveinstancestate().     // necessary when in two-pane layout.     log.w(log_tag, "creating remotefragment");     if(_datamanager == null) {         try {             _datamanager = (datamanager) getarguments().getserializable(remotedisplay.extraxmlparser);         }         catch (nullpointerexception e) {             _datamanager = null;             log.e(log_tag , "mxmlparser not passed arguments");         }     }     else         log.d(log_tag, "datamanager not null");     thiscontainer = container;     ... 

if force app restart second bit of code above, , make fragment launch first , third section, receive following logcat:

03-21 14:32:37.930: d/viewrootimpl(12915): viewpostimeinputstage action_down 03-21 14:32:38.090: d/remotedisplay(12915): selection 0 03-21 14:32:38.090: w/remotedisplay(12915): remotefrag not available 03-21 14:32:38.090: w/remotedisplay(12915): mdatamanager set 03-21 14:32:38.110: w/remotefragment(12915): creating remotefragment 03-21 14:32:38.110: d/remotefragment(12915): datamanager not null 03-21 14:32:38.110: d/remotefragment(12915): inflate now... 03-21 14:32:38.130: d/remotefragment(12915): finshed create view 

the confusing message messes "datamanager not null". means fragment did not try receive variable _datamanager activity passed argument, , initialization on top of class overwritten somewhere before oncreateview called old value.

where , can avoid this? way managed behave fragment want remove app recent activities.

sorry if turned out long... ;)

i found 1 quite obvious solution. add additional custom constructor fragment initializes variables:

    remotefragment() {     _datamanager=null;     _datamanagerregistered = false; } 

i still bit puzzled why not done custom constructor.


Comments

Popular posts from this blog

java - Could not locate OpenAL library -

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

sorting - opencl Bitonic sort with 64 bits keys -