java - Why does my SlidingTabLayout Restart my activity? -
i have 4 tabs, shown below when click tags
home
restarts home activity
. why that??
also, slidingtablayout
little choppy, have home activity
restart?
public class sectionspageradapter extends fragmentstatepageradapter { public sectionspageradapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int position) { //2 steps creating fragment, create fragment class , create ui fragment fragment = null; //we dont want return our place holder fragment instead want switch bewtween 3 //create switch case based on position applied or fragment selected switch (position) { case home: fragment = dashboard.newinstance("",""); break; case tags: fragment = tags.newinstance("",""); break; case my_recent: fragment = myposts.newinstance("",""); break; case top; fragment = top.newinstance("", ""); break; } return fragment; } @override public int getcount() { // show 3 total pages. return 4; } @override public charsequence getpagetitle(int position) { switch (position) { case 0: return "home"; case 1: return "top"; case 2: return "recent"; case 3: return "tags"; } return null; } } }
this because creating new instance of each fragment each time user changes tab. think should extend fragmentpageradapter
instead of fragmentstatepageradapter
.
if create fragments in sectionspageradapter
constructor, keep reference them member variables , return reference fragments in getitem()
method should stop recreating fragments.
maybe try this:
public class sectionspageradapter extends fragmentpageradapter { fragment mdashboardfragment; fragment mtagsfragment; fragment mmyrecentfragment; fragment mtopfragment; public sectionspageradapter(fragmentmanager fm) { super(fm); mhomefragment = dashboard.newinstance("",""); mtagsfragment = tags.newinstance("",""); mmyrecentfragment= myposts.newinstance("",""); mtopfragment = top.newinstance("", ""); } @override public fragment getitem(int position) { fragment fragment = null; //create switch case based on position applied or fragment selected switch (position) { case home: fragment = mdashboardfragment; break; case tags: fragment = mtagsfragment; break; case my_recent: fragment = mmyrecentfragment; break; case top; fragment = mtopfragment; break; } return fragment; } @override public int getcount() { // show 3 total pages. return 4; } @override public charsequence getpagetitle(int position) { switch (position) { case 0: return "home"; case 1: return "top"; case 2: return "recent"; case 3: return "tags"; } return null; }}}
bear in mind quite inefficient potentially creating fragments unnecessarily.
Comments
Post a Comment