android - Can a fragment's onAttach be called before the Activity's onCreate has finished? -
i experiencing rare (1 in few thousand sessions) crash trying track down. have activity which, during it's oncreate override, creates fragments doesn't show or attach of them:
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); setcontentview(r.layout.activity_main); mainmenufragment = new mainmenufragment(); locationfragment = new locationfragment(); mainpresenter = new mainpresenter(this); }
in code create "mainpresenter" comes library contains our business logic. presenter used onattach method of fragments:
@override public void onattach(activity activity) { super.onattach(activity); mainactivity mainactivity = (mainactivity) activity; mainpresenter = mainactivity.getmainpresenter(); mainpresenter.refreshui(); }
the issue is, getting null ptr exception in onattach. possible in rare cases fragment's onattach being executed before activities' oncreate has finished (i.e. mainpresenter null)?
update
here part of callstack leading crash, in case helpful:
android.app.activitythread.performlaunchactivity (activitythread.java:3253) android.app.activitythread.handlelaunchactivity (activitythread.java:3349) android.app.activitythread.handlerelaunchactivity (activitythread.java:5383) android.app.activitythread.access$1200 (activitythread.java:221) android.app.activitythread$h.handlemessage (activitythread.java:1800) android.os.handler.dispatchmessage (handler.java:102) android.os.looper.loop (looper.java:158) android.app.activitythread.main (activitythread.java:7225) java.lang.reflect.method.invoke (method.java) com.android.internal.os.zygoteinit$methodandargscaller.run (zygoteinit.java:1230) com.android.internal.os.zygoteinit.main (zygoteinit.java:1120)
yes. see documentation @ https://developer.android.com/reference/android/app/fragment.html#oncreate(android.os.bundle). fragment.onattach happens before fragment.oncreate , if documentation oncreate says activity can still in process of being created during lifecycle method, means can under construction in onattach, happens before oncreate.
as specific scenario, if entire oncreate of activity, fragments not hooked application @ all; have not been added via fragment manager. instantiated , cannot have android lifecycle calls yet. assume there more complex going on , fragments being re-added after configuration change or when crash.
Comments
Post a Comment