I'm switching my Android (Sherlock) Action Bar tabs like described in this question: Programmatically switch tabs in Android using ActionBarSherlock.
Is there any way to pass an argument to the called tab?
Thanks in anticipation!
Further information as requested by @semperfly:
My MainActivity has a ActionBar and implements ActionBar.TabListener
actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
tabCalculate = actionBar.newTab();
tabCalculate.setText("Calculate");
tabCalculate.setTabListener(this);
actionBar.addTab(tabCalculate);
tabArchive = actionBar.newTab();
tabArchive.setText("Archive");
tabArchive.setTabListener(this);
actionBar.addTab(tabArchive);
This is the implementation of the ActionBar.TabListener:
public void onTabSelected(Tab tab, android.support.v4.app.FragmentTransaction ft) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
SherlockFragment fragment = null;
if( tabCalculate.equals(tab) ) {
if( fragmentInput == null ) {
fragmentInput = new FragmentInput();
}
fragment = fragmentInput;
} else if ( tabArchive.equals(tab)) {
if( fragmentArchive == null ) {
fragmentArchive = new FragmentArchive();
}
fragment = new FragmentArchive();
}
fragmentTransaction.replace(R.id.fragmentsContainer, fragment);
fragmentTransaction.commit();
}
public void onTabUnselected(Tab tab, android.support.v4.app.FragmentTransaction ft) {
}
public void onTabReselected(Tab tab, android.support.v4.app.FragmentTransaction ft) {
}
My first approach was to load the InputFragment from the ArchiveFragment this way:
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
SherlockFragment fragment = new FragmentInput();
Bundle arguments = new Bundle();
arguments.putSerializable(FragmentInput.ARG_INPUTDATA_TO_LOAD, inputData);
fragment.setArguments(arguments);
fragmentTransaction.replace(R.id.fragmentsContainer, fragment);
fragmentTransaction.commit();
This worked perfectly fine, but didn't switch the active tab indicator in the ActionBar accordingly, the blue line below the archive tab was active albeit the input fragment was active.
Then I tried this method to switch tabs/fragments:
getSherlockActivity().getSupportActionBar().setSelectedNavigationItem(0);
This works but now I can't pass arguments to the tab to be activated.
Late, but you can use:
getActivity().getActionBar().setSelectedNavigationItem(1); //where 1 equals the 2nd tab
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With