Android session data instantiation related to hardware resource changes (rotation, etc) using fragments with tabs

可紊 提交于 2019-12-25 17:13:37

问题


I am developing an understanding of data instantiation as it applies to the ActionBar using tabs and fragments. The below code causes the following two errors,

1) "The method replace(int, Fragment, String) in the type FragmentTransaction is not applicable for the arguments (int, Tab1Fragment, String)" at

getSupportFragmentManager().beginTransaction().replace(android.R.id.content, Tab1,"Tab1Fragment_TAG").commit();

and,

2) "Cannot cast from Fragment to Tab1Fragment" at

Tab1Fragment Tab1 = (Tab1Fragment) getSupportFragmentManager().findFragmentByTag("Tab1Fragment_TAG");

Any suggestions regarding how I can make this work? Below is code as I have it now. Thanks.

public class MainActivity extends FragmentActivity 
{
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
            ActionBar actionBar = getActionBar();
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);            
            ActionBar.Tab tab1 = actionBar.newTab().setText("Tab1");
            Fragment tab1Fragment = new Tab1Fragment();
            tab1.setTabListener(new MyTabsListener(tab1Fragment));
                actionBar.addTab(tab1); 

    if(savedInstanceState == null) {
        Tab1Fragment Tab1 = new Tab1Fragment();
        Tab1.setArguments(getIntent().getExtras());
        getSupportFragmentManager().beginTransaction().replace(android.R.id.content, Tab1,"Tab1Fragment_TAG").commit();
    }else{
        Tab1Fragment Tab1 = (Tab1Fragment) getSupportFragmentManager().findFragmentByTag("Tab1Fragment_TAG");
    }           
  }
}

回答1:


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);
}
}


来源:https://stackoverflow.com/questions/24331434/android-session-data-instantiation-related-to-hardware-resource-changes-rotatio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!