Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
109 views
in Technique[技术] by (71.8m points)

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

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");
    }           
  }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...