1

My android app has a tabhost with 5 tabs. The problem is when I use setCurrentTab(2).Android start activity on the first tab then it start activity on the third tab. I find out that when I use method addTab of TabHost class,it also start activity in the first tab.

    if (mCurrentTab == -1) {
        setCurrentTab(0); 
    }

I read my thread in stackoverflow like:https://stackoverflow.com/questions/9978231/android-when-i-put-setcurrenttab-for-tabhost-it-loads-the-first-added-tab-ac

Cause My client requirement, I can't reorder tabs.When the TabActivity start,I only want start activity in the third tab ,How can I do that?

4 Answers 4

2

If you want to prevent loading of your fragment then put your in onVisible. Use this class UserVisibleHintFragment

It will help you to prevent advance loading.

0

use setCurrentTab(2) after addTab methods. Better use it at the last line of onCreate of your TabActivity

3
  • I tried it before, it isn't working, android start activity in first tab, then third tab
    – chinh
    May 22, 2013 at 3:05
  • oh ,, I misunderstood your question .. that is the natural flow to start the first tab
    – stinepike
    May 22, 2013 at 3:07
  • why do you need this .. a more clear description may help us to know what do you exactly want to achieve
    – stinepike
    May 22, 2013 at 3:08
0

enter image description here

As you can see from the pic that I attached,when app android starts,it will launch activity from third tab and it read user's mail address in android device then post it to server.After that server will return user_id,I will save user_id in app memory.When user click on first tab which has an activity that contain a webview ,I will get this user_id from app memory then pass to url of webview. If the activity in the first tab start first, the user_id will be blank.

0

Your question does not tell us how you are constructing your tabs. If you can post the bit of code on how you are building the tabs then we may be able to help you better.

Below is a simple way of building the tabs using TabHost. The tabHost.setCurrentTab(2); below just works fine...

TabHost tabHost = getTabHost();

// Tab for About
TabSpec aboutspec = tabHost.newTabSpec("About");
aboutspec.setIndicator("About", getResources().getDrawable(R.drawable.icon_about_tab));
aboutspec.setContent(new Intent(this, AboutActivity.class));

// Tab for Contacts
TabSpec contactsspec = tabHost.newTabSpec("Contacts");
contactsspec.setIndicator("Contacts", getResources().getDrawable(R.drawable.icon_contacts_tab));
contactsspec.setContent(new Intent(this, ContactsActivity.class));

// Tab for Resources
TabSpec resourcesspec = tabHost.newTabSpec("Resources");
resourcesspec.setIndicator("Resources", getResources().getDrawable(R.drawable.icon_resources_tab));
resourcesspec.setContent(new Intent(this, ResourcesActivity.class));

// Adding all TabSpec to TabHost
tabHost.addTab(aboutspec);
tabHost.addTab(contactsspec);
tabHost.addTab(resourcesspec);

// set the current tab to Resources
tabHost.setCurrentTab(2);

Hope this helps you resolve your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.