Reputation: 307
i have a problem with the onclick event for tabs at the ActionBar. I want to "outsource" the ActionBar-Tab logic to a class, so that i can reuse the ActionBar-Tabs-navigation in all activities.
Here is the outsourced "ActionBar-Tab" logic:
public class TabActivity extends Activity implements TabListener
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab);
// Set up the ActionBar to show tabs:
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Add Tabs:
actionBar.addTab(actionBar.newTab().setText("Dashboard").setTabListener(this),0,true);
actionBar.addTab(actionBar.newTab().setText("Search").setTabListener(this),1,false);
actionBar.addTab(actionBar.newTab().setText("Map").setTabListener(this),2,false);
}
@Override
public void onTabSelected(Tab arg0, FragmentTransaction arg1)
{
switch(arg0.getPosition())
{
case 0:
Intent dashboard = new Intent(this,DashBoardActivity.class);
startActivity(dashboard);
break;
case 1:
Intent suche = new Intent(this,SucheActivity.class);
startActivity(suche);
break;
case 2:
// Start Intent
break;
case 3:
// Start Intent
break;
case 4:
// Start Intent
break;
}
}
@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
}
@Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
}
}
And here is the DashBoardActivity which extends the TabActivity class:
public class DashBoardActivity extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dash_board);
}
}
The DashBoardActivity is also the Launcher Activity for my app.
So, my problem. When the App starts, the ActionBar navigation is successfully show as expected. I saw (via Log.i), that when the App starts, the ActionBar's onTabSelected() is called (without clicking it).
As you can see, i want to start different Activities depending on the Tab that was clicked.
The problem: The app starts,- onTabSelected is called automatically on launch,- and then the Activity "DashBoardActivity.class" get started. (but the current activity is DashBoardActivity!?!)
The started Activity extends the TabActivity,- but the ActionBar-Tabs are not show.
Is there a solution ?
Upvotes: 0
Views: 1414
Reputation: 230
Change this line:
actionBar.addTab(actionBar.newTab().setText("Dashboard").setTabListener(this),0,true);
to:
actionBar.addTab(actionBar.newTab().setText("Dashboard").setTabListener(this),0,false);
What I got from the Android Documentation:
public abstract void addTab (ActionBar.Tab tab, int position, boolean setSelected)
- setSelected - True if the added tab should become the selected tab.
Upvotes: 2