Android Fragment created twice orientation change -
my fragment being created twice, though activity adding fragment once content. happens when rotate screen. also, everytime fragment's oncreateview called, has lost of it's variable state.
public class mainactivity extends actionbaractivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); if (savedinstancestate == null) { // checking recreation getsupportfragmentmanager().begintransaction() .add(r.id.container, new apppanelfragment()) .commit(); } } }
oncreate in activity checks null savedinstancestate , if null add fragment can't see why fragment should created twice? putting breakpoint in if condition tells me ever get's called once activity shouldn't adding fragment multiple times. oncreateview of fragment still gets called each time orientation changes.
public class apppanelfragment extends fragment { private textview appnametext; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // method called several times view rootview = inflater.inflate(r.layout.fragment_app_panel, container, false); // 2nd call on method, appnametext null, why? appnametext = (textview) rootview.findviewbyid(r.id.app_name); appnametext.text = "my new app"; return view; }
i managed have variable state persisted using setretaininstance(true), real solution? expect fragment not created on orientation change.
the fragment
lifecycle similar activity
. default, yes, re-created during configuration change activity
does. that's expected behavior. setretaininstance(true)
(which use extreme caution if contains ui) view
destroyed , re-created, in case fragment
instance not destroyed -- view
.
Comments
Post a Comment