Home Ask Login Register

Developers Planet

Your answer is one click away!

eli February 2016

SlidingTabLayout, Restarts fragment On case 4 to 1?

my SlidingTabLayout restarts my fragment dashboard activity when switching from fragment tags to fragment dashboard, or position 4 to 0. Here is the code that I have written so far, can anyone shed some light on the situation ?

 public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
    public SectionsPagerAdapter(FragmentManager fm) {

    public Fragment getItem(int position) {
        //2 steps creating a fragment, create the fragment class and create the UI for it
        Fragment fragment = null;
        //we dont want it to return our place holder Fragment instead we want to switch bewtween 3
        //create a switch case based on the position applied or the fragment selected
        switch (position) {
            case HOME:
                fragment = Dashboard.newInstance("","");
            case TAGS:
                fragment = Tags.newInstance("","");
            case MY_RECENT:
                fragment = MyPosts.newInstance("","");
            case TOP:
                fragment = Top.newInstance("", "");
        return fragment;

    public int getCount() {
        // Show 3 total pages.
        return 4;

    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;


Mohit Suthar February 2016

You have to load all fragment in one time, you should call this method

int size=4;

after set adapter, size is the no of fragment which you want to load first time

Post Status

Asked in February 2016
Viewed 1,556 times
Voted 9
Answered 1 times


Leave an answer

Quote of the day: live life