android - Managing fragments -
i'm creating simple pageviewer-supported application, can switch thru fragments horizontally. application want use same background of fragments, difference between fragments text. i've made few fragments , application overly-solicitating, taking lot of space , using lot of memory. want optimize application in way space , memory needed running minimized. here mainactivity's code: (i'm creating class , different layout each fragment i'm not familiarized other way of doing so) --edit: i've forgot say, @ moment have 7 fragments application need 400-500 fragments optimization must.
public class mainactivity extends fragmentactivity { /** * {@link android.support.v4.view.pageradapter} provide * fragments each of sections. use * {@link android.support.v4.app.fragmentpageradapter} derivative, * keep every loaded fragment in memory. if becomes memory * intensive, may best switch * {@link android.support.v4.app.fragmentstatepageradapter}. */ sectionspageradapter msectionspageradapter; /** * {@link viewpager} host section contents. */ viewpager mviewpager; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // set action bar. // create adapter return fragment each of 3 // primary sections of app. msectionspageradapter = new sectionspageradapter(getsupportfragmentmanager()); // set viewpager sections adapter. mviewpager = (viewpager) findviewbyid(r.id.pager); mviewpager.setadapter(msectionspageradapter); // when swiping between different sections, select corresponding // tab. can use actionbar.tab#select() if have // reference tab. mviewpager.setonpagechangelistener(new viewpager.simpleonpagechangelistener() { @override public void onpageselected(int position) { } }); } public class sectionspageradapter extends fragmentpageradapter { public sectionspageradapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int position) {fragment f = null; switch(position){ case 0: { f = new dummysectionfragment();//yourfragment // set arguments here, if required bundle args = new bundle(); f.setarguments(args); break; } case 1: { f = new fragment2();//yourfragment // set arguments here, if required bundle args = new bundle(); f.setarguments(args); break; } case 2: { f = new fragment3();//yourfragment // set arguments here, if required bundle args = new bundle(); f.setarguments(args); break; } case 3: { f = new fragment4();//yourfragment // set arguments here, if required bundle args = new bundle(); f.setarguments(args); break; } case 4: { f = new fragment5();//yourfragment // set arguments here, if required bundle args = new bundle(); f.setarguments(args); break; } case 5: { f = new fragment6();//yourfragment // set arguments here, if required bundle args = new bundle(); f.setarguments(args); break; } case 6: { f = new fragment7();//yourfragment // set arguments here, if required bundle args = new bundle(); f.setarguments(args); break; } default: throw new illegalargumentexception("not many fragments: " + position); } return f; } @override public int getcount() { // show 3 total pages. return 7; } } /** * dummy fragment representing section of app, * displays dummy text. */ public static class dummysectionfragment extends fragment { /** * fragment argument representing section number * fragment. */ public static final string arg_section_number = "section_number"; public dummysectionfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_main_dummy, container, false); textview dummytextview = (textview) rootview.findviewbyid(r.id.section_label); dummytextview.settext(integer.tostring(getarguments().getint(arg_section_number))); return rootview; } } public static class fragment2 extends fragment { /** * fragment argument representing section number * fragment. */ public static final string arg_section_number = "section_number"; public fragment2() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment2, container, false); textview dummytextview = (textview) rootview.findviewbyid(r.id.section_label); dummytextview.settext(integer.tostring(getarguments().getint(arg_section_number))); return rootview; } } public static class fragment3 extends fragment { /** * fragment argument representing section number * fragment. */ public static final string arg_section_number = "section_number"; public fragment3() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment3, container, false); textview dummytextview = (textview) rootview.findviewbyid(r.id.section_label); dummytextview.settext(integer.tostring(getarguments().getint(arg_section_number))); return rootview; } } public static class fragment4 extends fragment { /** * fragment argument representing section number * fragment. */ public static final string arg_section_number = "section_number"; public fragment4() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment4, container, false); textview dummytextview = (textview) rootview.findviewbyid(r.id.section_label); dummytextview.settext(integer.tostring(getarguments().getint(arg_section_number))); return rootview; } } public static class fragment5 extends fragment { /** * fragment argument representing section number * fragment. */ public static final string arg_section_number = "section_number"; public fragment5() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment5, container, false); textview dummytextview = (textview) rootview.findviewbyid(r.id.section_label); dummytextview.settext(integer.tostring(getarguments().getint(arg_section_number))); return rootview; } } public static class fragment6 extends fragment { /** * fragment argument representing section number * fragment. */ public static final string arg_section_number = "section_number"; public fragment6() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment6, container, false); textview dummytextview = (textview) rootview.findviewbyid(r.id.section_label); dummytextview.settext(integer.tostring(getarguments().getint(arg_section_number))); return rootview; } } public static class fragment7 extends fragment { /** * fragment argument representing section number * fragment. */ public static final string arg_section_number = "section_number"; public fragment7() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment7, container, false); textview dummytextview = (textview) rootview.findviewbyid(r.id.section_label); dummytextview.settext(integer.tostring(getarguments().getint(arg_section_number))); return rootview; } } }
to manage fragments in activity, need use fragmentmanager. it, call getfragmentmanager() activity.
some things can fragmentmanager include:
get fragments exist in activity, findfragmentbyid() (for fragments provide ui in activity layout) or
findfragmentbytag() (for fragments or don't provide ui). pop fragments off stack, popbackstack() (simulating command user). register listener changes stack, addonbackstackchangedlistener().
Comments
Post a Comment