In my application, I'm showing 4 tabs using ViewPager.
tab1 | tab2 | tab3 | tab4
public class Sports_Swipe extends FragmentActivity implements ActionBar.TabListener {
private ViewPager viewPager;
private Sports_SwipeAdapter mAdapter;
private ActionBarSherlock mSherlock;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Tab1", "Tab2", "Tab3", "Tab4", "Tab5"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sports_swipe);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
mAdapter = new Sports_SwipeAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
///////////////////////////////////////////////////
public class Sports_SwipeAdapter extends FragmentPagerAdapter {
public Sports_SwipeAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
// Top Rated fragment activity
return new Tab1();
case 1:
// Games fragment activity
return new Tab2();
case 2:
// Movies fragment activity
return new Tab3();
case 3:
// Games fragment activity
return new Tab4();
case 4:
// Movies fragment activity
return new Tab5();
}
return null;
}
@Override
public int getCount() {
// get item count - equal to number of tabs
return 5;
}
}
/////////////////////////////////////
public class Tab1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_tab1, container, false);
//Here I get all the fields of Tab1
}
//I did this for remaining tabs also
My activity is extends FragmentActivity. Whenever the page loads I'm showing the data of tab1. Now if user clicks on tab2 I'm getting the data and showing. Now user clicks on tab3 then i'm showing the data of tab3. Up to this it is fine. Now when user goes back to the tab1 then the data is not available on the tab1. It is showing empty page.
How should I save the tab data or tab state.
Thanks