eli February 2016

Why does my SlidingTabLayout Restart my activity?

I have 4 tabs, as shown below when I click from Tags to Home it restarts the Home activity. Why is that??

Also, my slidingtabLayout is a little choppy, does this have anything to do with the home activity restart?

   public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    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("","");
                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;
    }
}

}

Answers


Robbie188 February 2016

This is because you are creating a new instance of each fragment each time the user changes tab. Also I think you should extend FragmentPagerAdapter instead of FragmentStatePagerAdapter.

If you create your Fragments in your SectionsPagerAdapter constructor, then keep a reference to them as member variables and then return a reference to your fragments in the getItem() method it should stop recreating fragments.

Maybe try something like 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 a switch case based on the position applied or the 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 that this could be quite inefficient as potentially creating fragments unnecessarily.

Post Status

Asked in February 2016
Viewed 3,063 times
Voted 6
Answered 1 times

Search




Leave an answer