Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: TabHost without TabActivity

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!

like image 473
paradroid666 Avatar asked Jul 02 '10 07:07

paradroid666


3 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());
like image 89
dcanh121 Avatar answered Nov 16 '22 14:11

dcanh121


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.

like image 26
CommonsWare Avatar answered Nov 16 '22 16:11

CommonsWare


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());
    }

}
like image 4
Lefteris E Avatar answered Nov 16 '22 16:11

Lefteris E