22

I have noticed that when using

actionBar.setSelectedNavigationItem(x)

in the onCreate() method of my Activity, the tab item at position 0 is always selected first and then the tab item at position x is loaded. This means that (since I'm using Fragments) 2 Fragments are loaded. One of them being unnecessary...

Here's my code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Determine which bundle to use; either the saved instance or a bundle
    // that has been passed in through an intent.
    Bundle bundle = getIntent().getExtras();
    if (bundle == null) {
        bundle = savedInstanceState;
    }

    // Initialize members with bundle or default values.
    int position;
    if (bundle != null) {
        position = bundle.getInt("selected_tab");
    } else {
        position = 0;
    }

    // Set the tabs.
    ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    Tab tab = actionBar
            .newTab()
            .setText("Tab 1")
            .setTabListener(
                    new TabListener<RendersGridFragment>(this, "1",
                            RendersGridFragment.class));
    actionBar.addTab(tab);

    tab = actionBar
            .newTab()
            .setText("Tab 2")
            .setTabListener(
                    new TabListener<RendersGridFragment>(this, "2",
                            RendersGridFragment.class));
    actionBar.addTab(tab);

    tab = actionBar
            .newTab()
            .setText("Tab 3")
            .setTabListener(
                    new TabListener<RendersGridFragment>(this, "3",
                            RendersGridFragment.class));
    actionBar.addTab(tab);

    actionBar.setSelectedNavigationItem(position);
}

It seems that the tab at position 0 is selected initially by default. But, as you can see, I'm passing bundles to make sure the last selected tab is still selected when the activity onCreate() method is run again.

For example, if the last selected tab is at position 2, the onCreate() runs and the tab at position is 0 is loaded, then the tab at position 2 is loaded.

How can I make sure the ActionBar doesn't select tab at position 0 first when using actionBar.setSelectedNavigationItem(position).

mdupls
  • 2,012
  • 1
  • 27
  • 40

6 Answers6

34

Use the other addTab calls to override this behaviour. You'll need to add the tab you want to be selected first (in your case, the tab at position 2). Relevant Javadoc

actionBar.addTab(tab2);
actionBar.addTab(tab0, 0, false);
actionBar.addTab(tab1, 1, false);
sastraxi
  • 1,330
  • 11
  • 21
  • That's perfect. I figured there was a simple fix. Thanks. – mdupls Mar 27 '12 at 20:00
  • It works, but I can see transition of ViewPager when activity is created. Is there any way, how to disable it? – sealskej Dec 06 '12 at 15:28
  • I'm not sure if I had the same problem of the author of question. In my case the function setSelectedNavigationItem(...) when called on onCreate of my Activity the fragment was instantiate twice or more. Changing for this solve my problem. Thanks. – Felipe Jan 09 '13 at 16:39
18

For any others looking to do this you can also set the tab to selected by setting the position and then set true or false to indicate which tab should be selected

actionBar.addTab(tab1, 0, false);
actionBar.addTab(tab2, 1, true);
actionBar.addTab(tab3, 2, false);

Here are the docs on this approach: http://developer.android.com/reference/android/app/ActionBar.html#addTab(android.app.ActionBar.Tab, int, boolean)

bkurzius
  • 4,020
  • 2
  • 34
  • 34
3

you can use below statment in activtiy onStart method:

protected void onStart() {
    super.onStart();
    actionBar.selectTab(mainTab);
}

which mainTab variable here is of type Tab. this way you need to define tabs as class-wide variables like this:

Tab mainTab, tab2,tab3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    //add tabs to action bar
    ....
}
VSB
  • 9,825
  • 16
  • 72
  • 145
2

If you have 3 tabs (i.e. tab 0, tab 1, tab 2) and want tab 1 to be preselected. Do this:

for (int i = 0; i < mFragmentPagerAdapter.getCount(); i++) {
    boolean preselected = (i == 1);
    actionBar.addTab(actionBar.newTab().setText(
        mFragmentPagerAdapter.getPageTitle(i)).setTabListener(this), preselected);
}

You will be using:

public abstract void addTab (ActionBar.Tab tab, boolean setSelected)

as per this API specification.

Percy Vega
  • 1,449
  • 1
  • 16
  • 13
0

bkurzius' answer helped me to fix a problem I was having with the same issue.

What I did was:

private final String TAB_SELECTED = "tab_selected"
...
private int mTabSelected;
...
mTabSelected = savedInstanceState.getInt(TAB_SELECTED);
...
final ActionBar actionbar = getActionBar();
...
actionbar.addTab(tab1, mTabSelected == 0);
actionbar.addTab(tab2, mTabSelected == 1);
actionbar.addTab(tab3, mTabSelected == 2);
...
outState.putInt(TAB_SELECTED, getActionBar().getSelectedNavigationIndex());

This way, the setSelected parameter is true only if mTabSelected is equal to the tab's index.

Fletcher Johns
  • 1,236
  • 15
  • 20
0

Percy Vega's reply seems to be the best working solution.

    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {

        boolean preselected = (i == ErrorDetails.tab_id);
        actionBar.addTab(
                actionBar.newTab()
                        .setText(mSectionsPagerAdapter.getPageTitle(i))
                        .setTabListener(this),preselected);
    }
Daniel
  • 11
  • 2