Below is a solution to retaining session data using fragment-tabs. I've uploaded the example project to Github. Here's the link:
https://github.com/portsample/FragmentTabRotation
MainActivity
public class MainActivity extends Activity{
private final static String TAB_KEY_INDEX = "TAB_KEY";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//put Actionbar in tab mode
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//set titles for tabs
ActionBar.Tab tab1 = actionBar.newTab().setText("Tab1");
ActionBar.Tab tab2 = actionBar.newTab().setText("Tab2");
ActionBar.Tab tab3 = actionBar.newTab().setText("Tab3");
//create instances of each of the fragments
Fragment tab1Fragment = new Tab1Fragment();
Fragment tab2Fragment = new Tab2Fragment();
Fragment tab3Fragment = new Tab3Fragment();
//attach those fragment instances to their respective tabs
tab1.setTabListener(new MyTabsListener(tab1Fragment));
tab2.setTabListener(new MyTabsListener(tab2Fragment));
tab3.setTabListener(new MyTabsListener(tab3Fragment));
//add each tab to the ActionBar
actionBar.addTab(tab1);
actionBar.addTab(tab2);
actionBar.addTab(tab3);
if (savedInstanceState == null){//...do nothing
}else if (savedInstanceState != null){
actionBar.setSelectedNavigationItem(savedInstanceState.getInt(TAB_KEY_INDEX,0));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
@Override
protected void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
outState.putInt(TAB_KEY_INDEX, getActionBar().getSelectedNavigationIndex());
}
}
MyTabsListener
public class MyTabsListener implements ActionBar.TabListener{
public Fragment fragment;
//default constructor
public MyTabsListener(Fragment fragment){
this.fragment = fragment;
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft){
//put code here if you want something special to happen when a user reselects a tab
}
//starts fragment when new tab is selected
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft){
ft.replace(R.id.fragment_placeholder, fragment);
}
//remove fragment when tab is unselected
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft){
ft.remove(fragment);
}
}
Tab1Fragment
public class Tab1Fragment extends Fragment{
String szSpecies;
public static int iSpeciesPosition;
Spinner spinnerSpecies;
@Override
public void onCreate(Bundle savedDataEntryInstanceState) {
super.onCreate(savedDataEntryInstanceState);
setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.tab1_fragment, container, false);
spinnerSpecies = (Spinner) v.findViewById(R.id.spinnerSpeciesxml);
if(savedInstanceState != null){
iSpeciesPosition = savedInstanceState.getInt("speciesPosition_key");
populateTab1Fragment(v);
}else if(savedInstanceState == null){
populateTab1Fragment(v);
}
return v;
}
@Override
public void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
iSpeciesPosition = spinnerSpecies.getSelectedItemPosition();
outState.putInt("speciesPosition_key", iSpeciesPosition);
}
public void populateTab1Fragment(View v){
//dropdown box
String [] szSpeciesArray = {"Sockeye","Marmot","Gumboot","Porpoise","Terrapin",};
Spinner spinnerSpecies = (Spinner) v.findViewById(R.id.spinnerSpeciesxml);
ArrayAdapter<String> arrayAdapterSpecies = new ArrayAdapter<String>(this.getActivity(), R.layout.spinnerstyle, szSpeciesArray);
arrayAdapterSpecies.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinnerSpecies.setAdapter(arrayAdapterSpecies);
spinnerSpecies.setSelection(iSpeciesPosition);
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…