android - Fragment is re created when ActionbarSherlock tab is selected -
i have fragment activity holds 3 fragments.
public class mainactivity extends sherlockfragmentactivity { /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); actionbar bar = getsupportactionbar(); bar.setnavigationmode(actionbar.navigation_mode_tabs); actionbar.tab tab1 = bar.newtab(); actionbar.tab tab2 = bar.newtab(); actionbar.tab tab3 = bar.newtab(); tab1.settext(""); tab1.seticon(r.drawable.abs__ic_menu_share_holo_dark); tab2.settext(""); tab2.seticon(r.drawable.abs__ic_voice_search); tab3.settext(""); tab3.seticon(r.drawable.abs__ic_cab_done_holo_dark); if (savedinstancestate == null) { tab1.settablistener(new mytablistener()); tab2.settablistener(new mytablistener()); tab3.settablistener(new mytablistener()); bar.addtab(tab1); bar.addtab(tab2); bar.addtab(tab3); } } private class mytablistener implements actionbar.tablistener { @override public void ontabselected(tab tab, fragmenttransaction ft) { switch (tab.getposition()) { case 0: feedsactivity frag = new feedsactivity(); ft.replace(android.r.id.content, frag); return; case 1: profileactivity frag2 = new profileactivity(); ft.replace(android.r.id.content, frag2); return; case 2: mymemoirsactivity frag3 = new mymemoirsactivity(); ft.replace(android.r.id.content, frag3); return; } } @override public void ontabunselected(tab tab, fragmenttransaction ft) { // todo auto-generated method stub } @override public void ontabreselected(tab tab, fragmenttransaction ft) { // todo auto-generated method stub } }
}
and here first fragment,
public class feedsactivity extends sherlockfragment { public static string[] maincategory; public static string[] maincategoryid; @override public view oncreateview(layoutinflater inflater, viewgroup group, bundle saved) { setretaininstance(true); return inflater.inflate(r.layout.activity_feeds, group, false); } @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); new getmaincategory(getactivity()).execute(); } }
when select second tab , select first tab async task in first fragment called again.how can retain state of first fragment view created once? have used setretaininstance(true) didnt work.
i solved issue using show , hide instead of attach , detach.
private class mytablistener implements actionbar.tablistener { @override public void ontabselected(tab tab, fragmenttransaction ft) { switch (tab.getposition()) { case 0: if (frag1 == null) { // if not, instantiate , add activity frag1 = fragment.instantiate(getapplicationcontext(), feedsactivity.class.getname()); ft.add(android.r.id.content, frag1, "feeds"); } else { // if exists, attach in order show ft.show(frag1); } return; case 1: if (frag2 == null) { // if not, instantiate , add activity frag2 = fragment.instantiate(getapplicationcontext(), profileactivity.class.getname()); ft.add(android.r.id.content, frag2, "profile"); } else { // if exists, attach in order show ft.show(frag2); } return; case 2: if (frag3 == null) { // if not, instantiate , add activity frag3 = fragment.instantiate(getapplicationcontext(), history.class.getname()); ft.add(android.r.id.content, frag3, "history"); } else { // if exists, attach in order show ft.show(frag3); } return; } } @override public void ontabunselected(tab tab, fragmenttransaction ft) { // todo auto-generated method stub if (frag1 != null) { // detach fragment, because 1 being attached switch (tab.getposition()) { case 0: ft.hide(frag1); return; case 1: ft.hide(frag2); return; case 2: ft.hide(frag3); return; } } } @override public void ontabreselected(tab tab, fragmenttransaction ft) { // todo auto-generated method stub } }
Comments
Post a Comment