This article is a follow up ofUse Android ActivityGroup within TabHost to show different Activity. As you probably noticed or read in the comments, the provided solution in the last article was less than ideal. Even though it does work, it uses multiple activies and therefor has a bigger memory consumption than necessary.

What happens when you use ActivityGroup with Tabhost

In the previous example we had one Activiy per tab. One tab was even holding two Activies and using ActivityGroup to switch between them. That's a total of five Activites that end up on the activity stack. Also the hierarchy was more nested than needed.

The improvement

So instead of using a Activity per Tab we can display a View in each. Thus we only need one Activity, all the events are caught by the class, so you need to find an ordered way of catching them. My example will implement the OnClickListener.

To keep the example short, we have two tabs. In the first one, there's a ViewFlipper which switches between two TextViews when clicked. In the second tab, there's just a bit of text and a button.

Here's the code:

  1. package com.unitedcoders.android.examples ;
  2. import android.app.TabActivity ;
  3. import android.os.Bundle ;
  4. import android.view.View ;
  5. import android.view.View.OnClickListener ;
  6. import android.widget.Button ;
  7. import android.widget.TabHost ;
  8. import android.widget.Toast ;
  9. import android.widget.ViewFlipper ;
  10. publicclassTabs extendsTabActivity implementsOnClickListener {
  11. ButtondoSomething ;
  12. TabHost tabHost ;
  13. ViewFlipper flipper ;
  14. @Override
  15. protectedvoidonCreate (Bundle savedInstanceState ) {
  16. super. onCreate (savedInstanceState ) ;
  17. setContentView (R. layout. tablayout_1 ) ;
  18. doSomething = ( Button )findViewById (R. id. btn_do_something ) ;
  19. doSomething. setOnClickListener ( this ) ;
  20. flipper = (ViewFlipper )findViewById (R. id. layout_tab_one ) ;
  21. flipper. setOnClickListener ( this ) ;
  22. tabHost =getTabHost ( ) ;
  23. tabHost. addTab (tabHost. newTabSpec ( "tab1" ). setIndicator ( "Tab1" ). setContent (R. id. layout_tab_one ) ) ;
  24. tabHost. addTab (tabHost. newTabSpec ( "tab2" ). setIndicator ( "Tab2" ). setContent (R. id. layout_tab_two ) ) ;
  25. tabHost. setCurrentTab ( 0 ) ;
  26. }
  27. @Override
  28. publicvoidonClick ( Viewv ) {
  29. // show a toast in second tab
  30. if (v ==doSomething ) {
  31. Toast. makeText (getApplicationContext ( ), "doing something", Toast. LENGTH_SHORT ). show ( ) ;
  32. }
  33. // toggle TextView in first tab
  34. if (v ==flipper ) {
  35. flipper. showNext ( ) ;
  36. }
  37. }
  38. }

and the layout that goes with it:

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <TabHostxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@android:id/tabhost"android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <LinearLayoutandroid:orientation="vertical"
  6. android:layout_width="fill_parent"android:layout_height="fill_parent">
  7. <TabWidgetandroid:id="@android:id/tabs"
  8. android:layout_width="fill_parent"android:layout_height="wrap_content"/>
  9. <FrameLayoutandroid:id="@android:id/tabcontent"
  10. android:layout_width="fill_parent"android:layout_height="fill_parent">
  11. <ViewFlipperandroid:id="@+id/layout_tab_one"
  12. android:layout_width="fill_parent"android:layout_height="fill_parent">
  13. <TextViewandroid:id="@+id/tabone1"android:layout_width="fill_parent"
  14. android:layout_height="fill_parent"android:text="this is a tab one"/>
  15. <TextViewandroid:id="@+id/tabone2"android:layout_width="fill_parent"
  16. android:layout_height="fill_parent"android:text="this is the other tab one"/>
  17. </ViewFlipper>
  18. <LinearLayoutandroid:id="@+id/layout_tab_two"
  19. android:orientation="vertical"android:layout_width="fill_parent"
  20. android:layout_height="fill_parent">
  21. <TextViewandroid:id="@+id/textview2"android:layout_width="fill_parent"
  22. android:layout_height="wrap_content"android:text="this is a tab two"/>
  23. <Buttonandroid:id="@+id/btn_do_something"android:text="do something"
  24. android:layout_width="fill_parent"android:layout_height="wrap_content"/>
  25. </LinearLayout>
  26. </FrameLayout>
  27. </LinearLayout>
  28. </TabHost>

From this UI you can use AsyncTask to do your processing and refresh the UI. This will keep your number of Activities and the memory consumption low.

As a little bonus, here are the screenshots of the hierachy view for the
current exampleand theprevious one.

The code is in theUCdroid project on github. You can run it on your device or emulator.

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. ThinkPHP中的find和select的区别
  2. 社交登录实现的数据库结构?
  3. 如何在jQuery Mobile页面中提交表单?
  4. [php]0-1背包动态规划
  5. php.ini与php-fpm.conf配置文件的区别
  6. PHP根据数组的值分组
  7. PHP扩展的URL Library快速入门
  8. Json调用JSON.parse:意外结束数据
  9. PHP+Apache环境安装与配置
  10. php $_SERVER中的SERVER_NAME 和HTTP_HOS