开发者

Android Two Tabs with same activity

This should be really simple but it aint working. What I want is two different tabs that use the same activity class. I don't care if they are sharing the same activity or if they each have their own instance. In this code I set the second tab with the same activity as the first, but only the first will load in the app. If I click the second tab I get a black screen:

//Create tabs
    Resources res = getResources();
    TabHost tabHost = getTabHost();
    TabHost.TabSpec s开发者_开发知识库pec;
    Intent intent;
    String tabTitle = getString(R.string.livevideo);

    // Initialize intent
    // Initialize tabspec for each tab and add it to host
    intent = new Intent().setClass(this, CameraListView.class);
    spec = tabHost.newTabSpec("live").setIndicator(tabTitle,res.getDrawable(R.drawable.livebtn)).setContent(intent);
    tabHost.addTab(spec);

    tabTitle = getString(R.string.videoplayback);
    intent = new Intent().setClass(this, CameraListView.class);
    spec = tabHost.newTabSpec("playback").setIndicator(tabTitle,res.getDrawable(R.drawable.playbackbtn)).setContent(intent);
    tabHost.addTab(spec);

Can't this just be a simple thing? I would think that making a new intent with the same activity would instantiate a second copy of the activity, but perhaps that isn't how Android works.


Try something like this:

TabHost tabHost = getTabHost();
TabSpec spec = null;

tabSpec = tabHost.newTabSpec("tabSpec");
tabSpec.setIndicator(someString, someDrawable);
tabSpec.setContent(new Intent(getApplicationContext(), CameraListView.class));
tabHost.addTab(tabSpec);

tabSpec = tabHost.newTabSpec("tabSpec");
tabSpec.setIndicator(someString, someDrawable);
tabSpec.setContent(new Intent(getApplicationContext(), CameraListView.class));
tabHost.addTab(tabSpec);

//EDIT In response to your question in comment. I don't know anything about any way to do what you're want to do. I had exactly the same problem. Finally I solved it by creating new Activity which inherited from firstTabActivity and I put it in second tab. In your case I think it'll be quite simple - SecondTabActivity extends FirstTabActivity and overrides method onListItemClick().


Use different tags for every tabspec like this;

tabSpec = tabHost.newTabSpec("tabSpec");
tabSpec = tabHost.newTabSpec("tabSpec2");

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜