MaTaKazer MaTaKazer - 2 months ago 15
Android Question

Fragments display getting reset

I have setup a list of fragments using

TabLayout
and
ViewPager
on MainActivity as such

public class MainActivity extends AppCompatActivity implements Fragment1.OnFragmentInteractionListener, Fragment2.OnFragmentInteractionListener, Fragment3.OnFragmentInteractionListener, Fragment4.OnFragmentInteractionListener
, Fragment5.OnFragmentInteractionListener{

TabLayout tabLayout;
ViewPager viewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();

setContentView(R.layout.activity_main);

viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.setAdapter(new CustomAdapater(getSupportFragmentManager(), getApplicationContext()));

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

tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#51ffff"));

tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
switch (tab.getPosition()) {
case 0:
tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#ced21f"));
break;
case 1:
tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#51ffff"));
break;
case 2:
tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#FF0000"));
break;
}
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabReselected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}

});

}

@Override
public void onFragmentInteraction(Uri uri) {

}


private class CustomAdapater extends FragmentPagerAdapter {

private String fragments[] = {"Control", "Connection", "Log", "Others", "Others2"};

public CustomAdapater(FragmentManager supportFragmentManager, Context applicationContext) {
super(supportFragmentManager);
}

@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new Fragment1();
case 1:
return new Fragment3();
case 2:
return new Fragment2();
case 3:
return new Fragment4();
case 4:
return new Fragment5();
default:
return null;
}
}

@Override
public int getCount() {
return fragments.length;
}

@Override
public CharSequence getPageTitle(int position) {
return fragments[position];
}
}
}


Somehow when I transit from Fragment1 to Fragment3 and back to Fragment1, the views on Fragment1 gets reset to original state(Like it was just created).

This only happens if I transit 2 fragment away from the current Fragment. For example 2->4 or 3->1.
Everything is fine if the transition is next to each other, for example 2->3 or 1->2.

This is my first android app so do pardon me.

It seems like it is creating a new fragment instead loading of existing ones

Answer

Your fragment get destroyed due to the fact that by default viewpager retain only one fragment at a time while moving around so you can simply use

viewPager.setOffscreenPageLimit(no_of_fragment_to_retain);

or

viewPager.setOffscreenPageLimit(3); // in your case

so when you come back from 4->1 or 3->1 you will get the previous stored state of your fragment. link to docs