Tab host - setCurrentTab(x) works for tab but not a content [Android]
Asked Answered
F

3

5

I have created tab app.. now I am playing with screen rotation. I tried to set tabHost.getTabWidget().setCurrentTab(1), which should show second tab (first is 0). The point is that second tab is shown as selected, but shown content is from first tab... How can I solve that?

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.main);

    myCommunicator = new Communicator();
    dbAdapter = new ToDoDBAdapter(this);


    if (getLastNonConfigurationInstance() != null) 
    {
        CurrentTab = (Integer)getLastNonConfigurationInstance();
        createView();
    }
    else
    {
        BuildDialog = ProgressDialog.show(this, "Loading", "Updating data...", true, false);
        BuildDialog.show();
        new LoadChannels().execute();
    }

}


private void createView()
{
        tabHost = (TabHost)findViewById(android.R.id.tabhost);
        tabHost.setOnTabChangedListener(this);

        Intent intent; 
        TabSpec spec;
        intent = new Intent().setClass(this, Channels.class);

        // TAB 1
        spec = tabHost.newTabSpec("kanali").setIndicator("Kanali",getResources().getDrawable(R.drawable.menu_channels)).setContent(intent);
        tabHost.addTab(spec);


        intent = new Intent().setClass(this, Currently.class);

        // TAB 2
        spec = tabHost.newTabSpec("trenutno").setIndicator("Trenutno",getResources().getDrawable(R.drawable.menu_current)).setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, Reminders.class);

        // TAB 3
        spec = tabHost.newTabSpec("opomniki").setIndicator("Opomniki",getResources().getDrawable(R.drawable.menu_reminder)).setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, About.class);

        // TAB 4
        spec = tabHost.newTabSpec("oprogramu").setIndicator("O programu",getResources().getDrawable(R.drawable.menu_about)).setContent(intent);
        tabHost.addTab(spec);

        tabHost.setBackgroundColor(Color.WHITE);
        tabHost.setCurrentTab(1); // Should always set content to second
}

@Override public Object onRetainNonConfigurationInstance() { return CurrentTab; }

@Override
public void onTabChanged(String tabId) {
    CurrentTab = tabHost.getCurrentTab();
}
public void onDestroy() {
    super.onDestroy();

    // Close the database
    try {
        dbAdapter.close();
    } catch (Exception e) {
        // TODO: handle exception
    }
  }

@Override
public void onConfigurationChanged(Configuration newConfig)
{
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.main);
    createView();
}
Fortunetelling answered 19/4, 2011 at 7:14 Comment(0)
C
16

Why are you calling getTabWidget()? You should use the setCurrentTab() on the tabHost itself.

Works fine here.

tabHost.setCurrentTab(1);
Cameraman answered 19/4, 2011 at 7:17 Comment(4)
Tried that before ... but same result with tabHost.setCurrentTab(1) or tanHost.getTabWidget().setCurrentTab(1)...Fortunetelling
@Fortunetelling Ehhh... I use the above code in my own application for swiping (listen for events, change tab if it's a valid swipe) and it works absolutely fine. If it doesn't work for you then your problem is somewhere else in the code.Cameraman
O.K. I have added code, handling the tab host... Hopefully this will help solving a problem. Tanks!Fortunetelling
Any particular reason that entire design is pretty different from the Android tutorial on it? Mine resembles developer.android.com/resources/tutorials/views/… quite closely, in particular the tabHost = getTabHost(); call might be necessary.Cameraman
B
1

Use setCurrentTabByTag(String tag); Depending upon the tag you mentioned would be the default tab, hope it helps, works (It's currectly working for this code!!)

private static final String A ="Kanali"; //required defualt tab name
.
.
.
tabHost.setCurrentTabByTag (A);

link

Boleyn answered 28/3, 2012 at 6:46 Comment(0)
H
0

Did you check TabHost in the debugger to be sure that all of the tabs were successfully added before you tried to set the currentTab? I had a similar issue and found that TabHost.setCurrentTab remains set to -1 if you try to reference an index that is outside of the range of tabs that were not successfully added. This behavior is not documented in the Android documentation.

Hackworth answered 25/4, 2011 at 19:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.