Saturday, 3 December 2016

How does TabLayout works

I have Tablayout inside MainActivity and Viewpager that shows three fragments When app started this process happens:

onCreate Fragment 1
onCreate Fragment 1
onActivityCreated Fragment 1
onResume Fragment 1
onActivityCreated Fragment 2
onResume Fragment 2
This happens when tapping at second tap:
onCreate Fragment 1
onActivityCreated Fragment 3
onResume Fragment 3
Tapping at third tap:
onSaveInstanceState Fragment 1
Tapping on second tap from third tap:
onCreate Fragment 1
onActivityCreated Fragment 1
onResume Fragment 1
Code
public class ViewPagerAdapter extends FragmentStatePagerAdapter {

private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();

public ViewPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int position) {
    return mFragmentList.get(position);
}

@Override
public int getCount() {
    return mFragmentList.size();
}

@Override
public CharSequence getPageTitle(int position) {
    return mFragmentTitleList.get(position);
}

public void addFragment(Fragment fragment, String title) {
    mFragmentList.add(fragment);
    mFragmentTitleList.add(title);
}
}
In MainActivity
        viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.tablayout);
    tabLayout.setupWithViewPager(viewPager);
}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(DishFragment.newInstance(1), "Кулинария");
    adapter.addFragment(DishFragment.newInstance(2), "Кондитерская");
    adapter.addFragment(DishFragment.newInstance(3), "Хлебобулочная");
    viewPager.setAdapter(adapter);
}
onCreate Fragment 1
onCreate Fragment 1
onActivityCreated Fragment 1
onResume Fragment 1
onActivityCreated Fragment 2
onResume Fragment 2

onCreate Fragment 1
onActivityCreated Fragment 3
onResume Fragment 3

onSaveInstanceState Fragment 1

onCreate Fragment 1
onActivityCreated Fragment 1
onResume Fragment 1



1st Answer;



ViewPager holds the adjacent Fragments in memory for quick transitions between pages. I'm thinking there's a fix for the recreated Fragments (since you're storing them in a list, add not creating them yourself more than once).

When app started this process happens:

I'm not sure about the doubled onCreate #1, but Fragment 1 and 2 are started
onCreate Fragment 1
onCreate Fragment 1
onActivityCreated Fragment 1
onResume Fragment 1
onActivityCreated Fragment 2
onResume Fragment 2
This happens when tapping at second tap:

Fragment 1 & 2 were already created, but Fragment 3 is now created


onCreate Fragment 1
onActivityCreated Fragment 3 
onResume Fragment 3 

Tapping at third tap:

The OS doesn't need to hold onto Fragment 1 anymore
onSaveInstanceState Fragment 1
Tapping on second tap from third tap:

Fragment 1 is created / resumed
onCreate Fragment 1
onActivityCreated Fragment 1
onResume Fragment 1
----------------------------------------------------------------------------------------------------------------------

Ok I see! But I found another misunderstanding. I use one fragment class for all three tabs. And it seems that items for next fragment are adding above previous items. Though I had created three different instances of the fragment. Is it in xml problem? maybe it is necessary to clear it? 

No comments:

Post a Comment