42

I want to create tabs without extending TabActivity. (The reason is that TabActivity cannot handle a custom titlebar as it seems). I have

public class startTab extends Activity {

 @Override
 public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mylayout);
        Resources res = getResources();
        LocalActivityManager mlam = new LocalActivityManager(this, false);
        TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
        tabHost.setup(mlam);
        TabHost.TabSpec spec;
        Intent intent;

    intent = new Intent().setClass(this, Show1.class);
    spec = tabHost.newTabSpec("Items").setIndicator("Items", res.getDrawable(R.drawable.items32_ldpi)).setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, Show2.class);
    spec = tabHost.newTabSpec("Users").setIndicator("Users",res.getDrawable(R.drawable.user32_ldpi)).setContent(intent);
    tabHost.addTab(spec);
}

}

The error I get is

    07-02 07:11:12.715: ERROR/AndroidRuntime(411): 
Caused by: java.lang.IllegalStateException: Activities can't be added until the containing group has been created.

The xml for the view is

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/tabhost" android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent" >
 <LinearLayout android:orientation="vertical"
  android:layout_width="fill_parent" android:layout_height="fill_parent"
  android:paddingTop="5dip">
  <TabWidget android:id="@android:id/tabs"
   android:layout_width="fill_parent" android:layout_height="fill_parent"></TabWidget>
  <FrameLayout android:id="@android:id/tabcontent"
   android:layout_width="fill_parent" android:layout_height="fill_parent"
   android:paddingTop="5dip">
  </FrameLayout>
 </LinearLayout>
</TabHost>

I read somewhere that I have to use a LocalActivityManager, I assume that I am missing something there. Anyone an idea?

Thanks!

Animesh
  • 4,926
  • 14
  • 68
  • 110
paradroid666
  • 553
  • 2
  • 6
  • 8
  • this tutorial might help you http://learnncode.wordpress.com/2013/12/18/how-to-use-tabwidget-with-fragments/ – Prachi Dec 23 '13 at 11:45

4 Answers4

86

Before calling tabHost.setup(mLocalActivityManager); you need to add this line.

mlam.dispatchCreate(savedInstanceState);
tabHost.setup(mlam );

similarly, you need to add for onResume,

mlam.dispatchResume(); 

onPause(),

 mlam.dispatchPause(isFinishing());
CJ Villa
  • 13
  • 3
dcanh121
  • 4,665
  • 11
  • 37
  • 84
  • 4
    But could you please explain why this works after adding these dispatchXXX() methods? Thanks very much:) – shihpeng May 18 '11 at 09:40
  • 1
    great!! but any idea why all these extra lines? – mudit Jan 05 '12 at 11:42
  • **It doesn't work for me..** I'm getting `android.util.AndroidRuntimeException: You cannot combine custom titles with other title features`. Note that my code is pretty similar as shown in question and I'm using **Custom Title feature**. – GAMA Apr 04 '12 at 13:07
  • It's an ugly solution. Another alternative is extends ActivityGroup, but the idea is not make the Activity extend strange things just because it has to display tabs. So maybe CommonsWare's answer about using views is the best? But it's a bit annoying to not be able just to use already existing activities... – User Apr 26 '12 at 10:19
  • It also doesn't work (or the solution is incomplete, at least). All my tabs are now empty. – User Apr 26 '12 at 10:30
  • is it necessary to use `dispatchStop()` within `onStop()` and `dispatchDestroy()` within `onDestroy` ? – Someone Somewhere Sep 12 '13 at 21:52
  • according to http://alvinalexander.com/java/jwarehouse/android/core/java/android/app/ActivityGroup.java.shtml it is needed, but I've seen a lot of null-pointer exception's due to dispatchDestroy() – Someone Somewhere Sep 12 '13 at 21:58
  • tnx! it works for me. I lost more time with Google Maps – Unmerciful Jul 04 '14 at 08:55
  • Saved me so much time Thanks – skryshtafovych Jul 24 '17 at 19:33
13

Please consider using Views as the contents of your tabs. Not only will this result in less code, less consumed heap space, less consumed stack space, and lower CPU utilization, it will also get you past this problem. Here are two examples showing this technique.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
4
public class ScoreboardActivity extends Activity {
    LocalActivityManager mlam;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scoreboard);
        mlam = new LocalActivityManager(this, false);
        mlam.dispatchCreate(savedInstanceState);
        TabHost th = (TabHost) findViewById(android.R.id.tabhost);
        th.setup(mlam);
        th.addTab(th.newTabSpec("Numpad").setIndicator("Numpad").setContent(R.id.tab1));
        th.addTab(th.newTabSpec("CardCount").setIndicator("CardCount").setContent(R.id.tab2));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_scoreboard, menu);
        return true;


    }
    @Override
    protected void onResume(){
        super.onResume();
        mlam.dispatchResume();
    }

    @Override
    protected void onPause(){
        super.onPause();
        mlam.dispatchPause(isFinishing());
    }

}
Lefteris E
  • 2,806
  • 1
  • 24
  • 23
1

Design considerations notwithstanding, the following does not work at all, and the API seems to indicate that setContent(Intent i) is valid. This works when the activity extends TabActivity, however, extending Activity and adding setup() call results in an exception at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:649)

Funny thing is, the LogCat suggests I forgot to call setup()

mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();

Intent tab1Intent = new Intent(this, ActivityOne.class);
Button tab1View = new Button(this);
tab1View.setText("Activity 1");
  mTabHost.addTab(mTabHost.newTabSpec("tab_1").setIndicator(tab1View).setContent(tab1Intent));
Andriy M
  • 76,112
  • 17
  • 94
  • 154
stanlick
  • 1,442
  • 3
  • 16
  • 29
  • This looks like it should have been a separate question. The issue you're likely running into is that you're not using a TabbedActivity, which means you need to call set up with the ActivityManager, as suggested above. That will then lead you to this issue, unless you dispatch the events to the ActivityManager properly. – shortstuffsushi Sep 03 '15 at 14:33