Use Android ActivityGroup within TabHost to show different Activity

Use Android ActivityGroup within TabHost to show different Activity


there is an updated version to this article with some improvements. please also read: Android ViewFlipper within TabHost for Tabs with different Views ... and better memory footprint.

When you're using a TabHost each tab has it's own Activity. Now image you want to change the Activity for a certain tab. If you just go on and create a new Activity and display it, your Tab Layout is no longer visible.

For that reason you need a ActivityGroup within the Tab where you want to change the Activity.
An ActivityGroup is: A screen that contains and runs multiple embedded activities.

Let's look at this at a real life example. An Android app that shows your podcasts. In the first Activity you get to see all podcasts by subscription. If you touch the subscription, you see the single podcasts you've downloaded for that subscription.

The Tabhost contains three tabs, one for the MediaPlayer, one for the archive and one for available downloads.

  1.         TabHost tabHost  = getTabHost ( ) ;
  2.  
  3.         tabHost. addTab (tabHost. newTabSpec ( "tab1" ). setIndicator ( "Player" ). setContent (
  4.                  new Intent ( this, PlayerActivity. class ) ) ) ;
  5.         tabHost. addTab (tabHost. newTabSpec ( "tab2" ). setIndicator ( "Archive" ). setContent (
  6.                  new Intent ( this, ArchiveGroup. class ) ) ) ;
  7.         tabHost. addTab (tabHost. newTabSpec ( "tab3" ). setIndicator ( "Updates" ). setContent (
  8.                  new Intent ( this, DownloadList. class ) ) ) ;

The ArchiveGroup takes care which Activity is shown in the second tab. With setContentView you can bring the View to the front.

  1.          View view  = getLocalActivityManager ( ). startActivity ( "ArchiveActivity",
  2.                  new Intent ( this, ArchiveActivity. class ). addFlags (Intent. FLAG_ACTIVITY_CLEAR_TOP ) ). getDecorView ( ) ;
  3.        
  4.         setContentView (view ) ;

Now all you need to do is to bring another View to the front after an action is triggered. In this case, after a ListItem is clicked.

  1.          String album  =  ( String ) getListView ( ). getItemAtPosition (position ) ;
  2.         Intent intent  =  new Intent (getApplicationContext ( ), ArchiveAlbums. class ) ;
  3.         intent. putExtra ( "album", album ) ;
  4.         intent. addFlags (Intent. FLAG_ACTIVITY_CLEAR_TOP ) ;
  5.        
  6.          View view  = ArchiveGroup. group. getLocalActivityManager ( ). startActivity ( "ShowPodcasts", intent ). getDecorView ( ) ;
  7.        
  8.         ArchiveGroup. group. setContentView (view ) ;

You need to tell the ActivityGroup which view is to be on top and set the  appropriate Flag, so that the View will be on top.

Of course now you have to keep track which activity is in front, how they behave and what happens if the back button is pressed. But that gives you room for customizing the behavior.

The full code can be found on github in the GpodRoid project.

Another good tutorials are by H. Larsen and Eric Harlow.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值