Android: TabHost without TabActivity

I want to create tabs without expanding TabActivity. (The reason is that TabActivity cannot handle the custom title bar 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. 

xml for presentation

 <?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 should use the LocalActivityManager, I assume that something is missing there. Any idea?

Thank!

+42
android android-tabhost
Jul 02 '10 at 7:15
source share
4 answers

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()); 
+86
Feb 23 '11 at 23:45
source share

Please use Views as the content of your tabs. Not only will this lead to less code, less heap space consumed, less stack space consumption and lower CPU usage, it will also help you overcome this problem. Here are two examples that demonstrate this technique.

+13
Jul 02 2018-10-10T00:
source share
 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()); } } 
+4
Jan 01 '13 at 15:47
source share

Despite design considerations, the following does not work at all, and the API seems to indicate that setContent(Intent i) valid. This works when the action continues with TabActivity , however, extending the Activity and adding setup() call results to exception at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:649)

Funny, LogCat suggests forgetting 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)); 
+1
Aug 19 '10 at 15:55
source share



All Articles