android - Why on landscape/potrait orientation change fragments get attached before onCreate called on main activity? -
i have main activity embeds fragment:
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); vd = virtualdatabasetableprovider.getinstance(getapplicationcontext()); fm = getsupportfragmentmanager(); fm.popbackstack(null, fragmentmanager.pop_back_stack_inclusive); //create layout , add fragments setcontentview(r.layout.main_window); listfragment listfragment= new listfragment(); android.support.v4.app.fragmenttransaction ft = fm.begintransaction(); ft.replace(r.id.fragment_pane, listfragment, "list"); //ft.replace(r.id.fragment_pane, listfragment); ft.addtobackstack(null); ft.commit(); //initialising buttons imgbtnfontinc = (imagebutton) findviewbyid(r.id.imgbtnupfont); imgbtnfontinc.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if(textviewattached){ try{ //some text resize } }catch (nullpointerexception npe){ log.e(tag, "error on calling text resize"); log.e(tag, npe.getmessage()); log.e(tag, npe.getstacktrace().tostring()); } } } } ); /* more buttons code..... */ imgbtnfontdec.setvisibility(view.gone); imgbtnfontinc.setvisibility(view.gone); /* saved state handling recover detailed text screen*/ if(savedinstancestate != null){ if (savedinstancestate.containskey("uuid")){ try{ string uuid = savedinstancestate.getstring("uuid"); if (uuid != null){ initextscreen(uuid); } }catch (exception e){ log.e(tag, "unable return text"); } } }
text initialised function:
private void inittextscreen(string stringid){ bundle data = new bundle(); data.putstring("uuid", stringid); textscreenfragment textfragment = new textscreenfragment(); textfragment.setarg1ments(data); if(fm == null){ fm = getsupportfragmentmanager(); } android.support.v4.app.fragmenttransaction ft = fm.begintransaction(); ft.setcustomanimations( r.anim.animation_enter, r.anim.animation_exit); ft.replace(r.id.fragment_pane, textfragment, "textfragment"); ft.addtobackstack(null); ft.commit(); }
i handled buttons visibility in main activity simple callback textscreenfragment. callback in main activity:
public void ontextviewattached() { textviewattached = true; mainactivity.this.imgbtnfontdec.setvisibility(view.visible); mainactivity.this.imgbtnfontinc.setvisibility(view.visible); }
callback called in textscreenfragment:
@override public void onattach(activity activity) { super.onattach(activity); if (!(activity instanceof callbacks)) { throw new illegalstateexception( "activity must implement fragment's callbacks."); } else { listener = (callbacks) activity; listener.ontextviewattached(); } } public interface callbacks { /** * callback when item has been selected. */ public void ontextviewattached(); }
it works, when put android phone switch potrait/landscape mode: onattached in fragment called way before oncreate in main activity , button objects. how can fragment attached on main activity before oncreate called in main activity?
i attached particular fragment @ end of oncreate method after buttons initialized,but why onattach in fragment called before attach fragment , null exception, because button objects not initialized in oncreate? how possible?
when comment out:
`// mainactivity.this.imgbtnfontdec.setvisibility(view.visible); //mainactivity.this.imgbtnfontinc.setvisibility(view.visible);`
in callback function public void ontextviewattached()
, no more crashes, still noticed onattach called before main activity created , called twice: 1 time uninitialised activity hell knows (every element of main activity either null or has default values), second time when fragment attached main activity oncreate.
i made conclusion on orientation switch fragments gets atached uninitialised activity. missing something, on orientation change should call other function before oncreate in main activity buttons layout?
is kind of fragment automated attach behaviour not aware of , take advantage of?
what life cycle of activity fragment attached it, because onattach called in fragment before main activity created seems counter intuitive.
Comments
Post a Comment