android setting default tab in tab-activity

2019-08-28 05:06发布

问题:

android TabActivity launches the FragmentActivity associated with the first tab added in the sequence before setting tabhost.setCurretnTab(4);

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab_main);
    try
    { 
        DataSource.ObjContext = this.getApplicationContext();
        DataSource.ObjTabBarActivity = this;
        DataSource.ObjSharedPreferences = this.getSharedPreferences("com.example", Context.MODE_PRIVATE);
        if(NetworkStat)
        {
            new LocationUpdates(this);

            this.setTabs();

        }
        else
        {
            Log.d("in TabBarActivity", "Network failure");
            Toast.makeText(this.getApplicationContext(), "Network failure", Toast.LENGTH_SHORT).show();
        }
    }
   catch(Exception ex)
    { 

    }


}

private void setTabs()
{
    addTab("Clubs", R.drawable.tab_clubs,  FragmentStackClubsActivity.class);
    addTab("Events", R.drawable.tab_events, FragmentStackEventsActivity.class);
    addTab("Rate", R.drawable.tab_rate, FragmentStackRateActivity.class);
    addTab("Loyalty", R.drawable.tab_loyalty, FragmentStackLoyaltyActivity.class);
    addTab("Setting", R.drawable.tab_settings, FragmentStackSettingsActivity.class);
    if(DataSource.ObjSharedPreferences.getString(DataSource.LOGIN_TAG, "false").equalsIgnoreCase("false"))
    {

        getTabHost().setCurrentTab(4);
        DataSource.disableTabBar();
    }
    else
    {

    }

}
private void addTab(String labelId, int drawableId, Class<?> c)
{
    TabHost tabHost = getTabHost();
    Intent intent = new Intent(this, c);
    TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId); 

    View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
    TextView title = (TextView) tabIndicator.findViewById(R.id.title);
    title.setText(labelId);
    ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
    icon.setImageResource(drawableId);

    spec.setIndicator(tabIndicator);
    spec.setContent(intent);
    tabHost.addTab(spec);
}

but the problem is that it starts the first tab initially and then switches to 5th tab in this way a thread gets started from the first tab and that's what i don't want i.e if the user is not logged in i wanna redirect the user to login(settings) tab. any help in this regard is highly appreciated......

回答1:

public void setCurrentTab (int index)
public void setCurrentTabByTag (String tag)

you can like this

if (isNotLogin) {
     tabHost.setCurrentTabByTag("Setting");
}

after addTab()

http://developer.android.com/reference/android/widget/TabHost.html#setCurrentTab(int)