android - life cycle callbacks of fragments are not called accordingly -
i have main activity view pager , 3 action tabs "fragment". , implemented life cycle callbacks each of main activity , 3 fragments. , in each of life cycle call placed log statements indicates life cycle callback being called know how action bars view pager behaves. @ run time perceived strange behaviour cant understand or attribute reason.
first behaviour
when app fisrt starts receive: 02-08 15:16:14.771 32243-32243/com.example.com.vpager_00 w/mainactivity: oncreate() 02-08 15:16:14.901 32243-32243/com.example.com.vpager_00 w/mainactivity: onstart() 02-08 15:16:14.901 32243-32243/com.example.com.vpager_00 w/mainactivity: onresume() 02-08 15:16:14.941 32243-32243/com.example.com.vpager_00 w/frag_1: onattach() 02-08 15:16:14.941 32243-32243/com.example.com.vpager_00 w/frag_1: oncreate() 02-08 15:16:14.941 32243-32243/com.example.com.vpager_00 w/frag_1: oncreateview() 02-08 15:16:14.941 32243-32243/com.example.com.vpager_00 w/frag_1: onstart() 02-08 15:16:14.941 32243-32243/com.example.com.vpager_00 w/frag_1: onresume() 02-08 15:16:14.941 32243-32243/com.example.com.vpager_00 w/frag_2: onattach() 02-08 15:16:14.941 32243-32243/com.example.com.vpager_00 w/frag_2: oncreate() 02-08 15:16:14.941 32243-32243/com.example.com.vpager_00 w/frag_2: oncreateview() 02-08 15:16:14.941 32243-32243/com.example.com.vpager_00 w/frag_2: onstart() 02-08 15:16:14.941 32243-32243/com.example.com.vpager_00 w/frag_2: onresume()
and cant find reason why life cycle callbacks of mainactivty, frag_1 , frag_2 called not frag_3? explaination?
2nd behaviour
occured when touched tab3 "frag_3", received: 02-08 15:16:36.031 32243-32243/com.example.com.vpager_00 d/viewrootimpl: viewpostimeinputstage action_down 02-08 15:16:36.121 32243-32243/com.example.com.vpager_00 w/frag_3: onattach() 02-08 15:16:36.121 32243-32243/com.example.com.vpager_00 w/frag_3: oncreate() 02-08 15:16:36.121 32243-32243/com.example.com.vpager_00 w/frag_3: oncreateview() 02-08 15:16:36.121 32243-32243/com.example.com.vpager_00 w/frag_3: onstart() 02-08 15:16:36.121 32243-32243/com.example.com.vpager_00 w/frag_3: onresume() 02-08 15:16:36.461 32243-32243/com.example.com.vpager_00 w/frag_1: onpause() 02-08 15:16:36.461 32243-32243/com.example.com.vpager_00 w/frag_1: onstop() 02-08 15:16:36.471 32243-32243/com.example.com.vpager_00 w/frag_1: ondestroy()
i think being life cycle callbackes called understandable, why life cycle callbacks of frag_1 called, think, beside callbacks of frag_3 , frag_1, aslo frag_2 callbacks must have been called because frag_2 in onresume() state lately.
any explaination?
mainactivity
public class mainactivity extends appcompatactivity { private final string tag = this.getclass().getsimplename(); private toolbar mtb = null; private tablayout mtl = null; private viewpager mvp = null; private vpageradapter mvpadapter = null; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); log.w(tag, "oncreate()"); this.initviews(r.layout.act_main); this.initobjs(); } private void initobjs() { this.mvpadapter = new vpageradapter(getsupportfragmentmanager(), this.mtl.gettabcount()); this.mvp.setadapter(this.mvpadapter); this.mvp.addonpagechangelistener(new tablayout.tablayoutonpagechangelistener(this.mtl)); this.mtl.setontabselectedlistener(new tablayout.ontabselectedlistener() { @override public void ontabselected(tablayout.tab tab) { mvp.setcurrentitem(tab.getposition()); } @override public void ontabunselected(tablayout.tab tab) { } @override public void ontabreselected(tablayout.tab tab) { } }); } private void initviews(int rootview) { setcontentview(rootview); this.mtb = (toolbar) findviewbyid(r.id.toolbar); this.mtl = (tablayout) findviewbyid(r.id.tab_layout); this.mvp = (viewpager) findviewbyid(r.id.pager); setsupportactionbar(this.mtb); this.mtl.addtab(this.mtl.newtab().settext("tab 1")); this.mtl.addtab(this.mtl.newtab().settext("tab 2")); this.mtl.addtab(this.mtl.newtab().settext("tab 3")); this.mtl.settabgravity(tablayout.gravity_fill); } @override protected void onstart() { super.onstart(); log.w(tag, "onstart()"); } @override protected void onresume() { super.onresume(); log.w(tag, "onresume()"); } @override protected void onpause() { super.onpause(); log.w(tag, "onpause()"); } @override protected void onstop() { super.onstop(); log.w(tag, "onstop()"); } @override protected void ondestroy() { super.ondestroy(); log.w(tag, "ondestroy()"); }
}
frag_1
public class frag_1 extends fragment { private final string tag = this.getclass().getsimplename(); @override public void onattach(context context) { super.onattach(context); log.w(tag, "onattach()"); } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); log.w(tag, "oncreate()"); } @nullable @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { log.w(tag, "oncreateview()"); return inflater.inflate(r.layout.frag_1, container, false); } @override public void onstart() { super.onstart(); log.w(tag, "onstart()"); } @override public void onresume() { super.onresume(); log.w(tag, "onresume()"); } @override public void onpause() { super.onpause(); log.w(tag, "onpause()"); } @override public void onstop() { super.onstop(); log.w(tag, "onstop()"); } @override public void ondestroy() { super.ondestroy(); log.w(tag, "ondestroy()"); }
}
frag_2:
public class frag_2 extends fragment { private final string tag = this.getclass().getsimplename(); @override public void onattach(context context) { super.onattach(context); log.w(tag, "onattach()"); } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); log.w(tag, "oncreate()"); } @nullable @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { log.w(tag, "oncreateview()"); return inflater.inflate(r.layout.frag_2, container, false); } @override public void onstart() { super.onstart(); log.w(tag, "onstart()"); } @override public void onresume() { super.onresume(); log.w(tag, "onresume()"); } @override public void onpause() { super.onpause(); log.w(tag, "onpause()"); } @override public void onstop() { super.onstop(); log.w(tag, "onstop()"); } @override public void ondestroy() { super.ondestroy(); log.w(tag, "ondestroy()"); }
}
frag_3:
public class frag_3 extends fragment { private final string tag = this.getclass().getsimplename(); @override public void onattach(context context) { super.onattach(context); log.w(tag, "onattach()"); } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); log.w(tag, "oncreate()"); } @nullable @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { log.w(tag, "oncreateview()"); return inflater.inflate(r.layout.frag_3, container, false); } @override public void onstart() { super.onstart(); log.w(tag, "onstart()"); } @override public void onresume() { super.onresume(); log.w(tag, "onresume()"); } @override public void onpause() { super.onpause(); log.w(tag, "onpause()"); } @override public void onstop() { super.onstop(); log.w(tag, "onstop()"); } @override public void ondestroy() { super.ondestroy(); log.w(tag, "ondestroy()"); }
}
in viewpager, fragments being created ahead of time.
that means, frag_2
not visible yet (because frag_1
covers entire screen), still creates view beforehand, scrolling frag_2
smooth.
by default, viewpager preloads 1 fragment on each side. if set start position of viewpager frag_2
, load frag_1
, frag_2
well, because neighbors.
you'll notice frag_3
created when swipe frag_2
you can increase number of preloaded fragments calling viewpager.setoffscreenpagelimit(int limit)
.
note min. number 1, can not disabled.
Comments
Post a Comment