Android fragment -- OnDestroy being called when switching to a different tab followed by OnCreate when returning to the tab? -
i have tabbed android app. when switch away tab, seeing ondestroy called corresponding fragment. when come tab, seeing oncreate called same fragment object. same object, not instance of same class. if switch away tab, ondestroy called again, followed 3rd phone call oncreate if homecoming tab, , on.
android docs appear state should not happen.
does indicate architectural problem app? if it's relevant, using mono, , setting retaininstance true in onactivitycreated.
edit: code below activity wraps tabs.
protected override void oncreate(bundle bundle) { base.oncreate(bundle); maincontext maincontext = maincontext; tabcontext tabcontext = maincontext.tabcontext; // tab custom model object; not related android tabs. list<tab> tabs = tabcontext.tabs; foreach (tab tab in tabs) { string displaystring = tab.displaystring; string withunderscores = displaystring.replace(' ', '_'); type fragmenttype = type.gettype(assembly.getexecutingassembly().getname().name + "." + withunderscores + "fragment"); this.addtab(tab, (fragment) activator.createinstance(fragmenttype)); } actionbar bar = this.actionbar; bar.navigationmode = actionbarnavigationmode.tabs; bar.setdisplayshowtitleenabled(false); bar.setdisplayshowhomeenabled(false); setcontentview(resource.layout.maintabactivitylayout); if (bundle != null) { int index = bundle.getint("index"); actionbar.setselectednavigationitem(index); } } private void addtab(tab tab, fragment fragment) { actionbar bar = this.actionbar; android.app.actionbar.tab droidtab = bar.newtab(); droidtab.settag(tab.tostring()); // omitting code sets icon , display text. droidtab.tabselected += (sender, e) => { e.fragmenttransaction.replace(resource.id.fragmentcontainer, fragment, tab.tostring()); }; this.actionbar.addtab(droidtab); }
it calling ondestroy
because replacing fragment each time alter tab
e.fragmenttransaction.replace(resource.id.fragmentcontainer, fragment, tab.tostring());
as see using 1 layout(resource.id.fragmentcontainer
) replacing fragment hence reusing after alter tab , releasing memory of replaced fragment calling ondestroy
.
android android-fragments tabs mono monodroid
No comments:
Post a Comment