ITKeyword,专注技术干货聚合推荐

注册 | 登录

android - TabHost w/ Activities vs. ActionBar w/ Fragments

itPublisher 分享于

2021腾讯云限时秒杀,爆款1核2G云服务器298元/3年!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1062

2021阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址https://www.aliyun.com/minisite/goods

推荐:android之TabHost

package com.example.tab;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.Menu;import andro

I've found some discussions related to this issue but nothing clear or definitive.

Building a tab-based application, one seems forced to choose between (A) using TabHost with Activities for each screen, and (B) using the ActionBar with Fragments for each screen. This raises a few questions:

1) Is that dilemma for real, or can one use the ActionBar with different Activities.

2) If the dilemma is for real, why are things set up this way? Is Google planning to deprecate TabHost and the multiple-Activities approach to tab-based navigation? Is there something unsavory about the multiple-Activities approach?

3) If both approaches will continue to be supported, what are the pros and cons of each? If I go with ActionBar+Fragments, will I run into any gotchas down the line? For example, when I want one of my tabbed screens to slide over/pop-on an additional screen/Fragment, will I get funny behavior switching to/from tabs in the ActionBar?

android android-fragments android-actionbar android-tabhost
|
  this question
asked Nov 16 '12 at 15:56 Jonathan 491 2 6 13

 | 

2 Answers
2

You also have the third option of using a TabHost with Fragments. This makes it to where you only have one activity and multiple fragments as the children.

http://developer.android.com/reference/android/support/v4/app/FragmentTabHost.html

推荐:android tabhost学习

public class TabWidgetActivity extends TabActivity { /** Called when the activity is first created. */ @Override public void onCreate(B

I have created a simple example using this class.

https://github.com/marsucsb/nested-fragments


|
  this answer
answered Nov 16 '12 at 19:47 Marco RS 5,502 2 20 30 2   Right, that is a third option. Does it have any advantages over ActionBar+Fragments though? The docs recommend the ActionBar for tabbed navigation for consistency and other reasons, and it sounds like TabHost may get deprecated. As long as I'm doing the whole application in fragments, is there any reason to go TabHost? –  Jonathan Nov 17 '12 at 16:45 1   I think it is just a matter of preference and need specific for your application. I personally do not usually use TabHost over the ActionBar+Fragments+ViewPager approach because I like the effect of being able to swipe through fragment pages. –  Marco RS Nov 17 '12 at 20:19      Cool, thanks! Btw, a different solution I'm fiddling with right now: use ActionBar tabs, but with a separate Activity for each tab by building an identical ActionBar for each Activity. With the animations suppressed when switching between activities, it's working surprisingly well. Feels hacky, but might end up being more elegant than all-Fragments when it comes to navigating forward/back under a single tab (ViewPager may not serve my purposes, need something more like iPhone's UINavigationController). See any impending doom for this approach? –  Jonathan Nov 18 '12 at 5:03      Wow, that set up is a little hard to imagine. I think you should be okay but I always worry about maintainability. Have you looked into possibly using fragments with a ViewFlipper to flip between views and a stack that maintains the viewflipper state. Ive been slowly trying to get alway from using all activities. It seems like fragments are the way to go due to how easy it is to reuse them and the role they play when supporting tablets. –  Marco RS Nov 21 '12 at 1:28      ViewFlipper is a good idea, but the navigation forks--I'll need to flip to different fragments depending on what the user selects. –  Jonathan Nov 24 '12 at 17:23  |  show more comments

I'm with you. I began developing "Droid Bones" (be sure to use the quotes) before the advent of Fragments. When I began looking at that architecture, it appeared that it would not accommodate my structural requirements. That said, I've found the multiple-activity approach to be very fluid and flexible. My concern is that some of the TabHost documentation appears to have 'gone missing' of late!


|
  this answer
edited Nov 18 '12 at 23:19 answered Nov 16 '12 at 16:06 gssi 2,146 4 12 21      Can you elaborate on the structural limitations of the Fragments approach? What did "Droid Bones" require that couldn't be done in Fragments? (Also, docs disappearing is concerning! What's gone missing?) –  Jonathan Nov 17 '12 at 16:48 1   The main concern I had was that I employ a couple of spinners above the TabWidget and I couldn't find a way of doing this with the ActionBar construct. Also, I was concerned that I also employ a TabWidget in the child Activity frames which I couldn't see the ActionBar accommodating. –  gssi Nov 18 '12 at 23:45      As for the missing docs, there used to be a sample project named "HelloTabWidget". I don't see this anywhere in the SDK samples directory. –  gssi Nov 18 '12 at 23:47      Apparently the major problem with ActionBar tabs is the back button logic, which doesn't behave well. Seems you have to create a custom back stack and handle the back-button logic yourself, in an override of onBackPressed(). Weak sauce, Google. –  Jonathan Nov 20 '12 at 15:34      There's some discussion of the back stack issue here and here. –  Jonathan Nov 20 '12 at 16:15

 | 

推荐:Android的TabHost

哎,看来我的学习能力真是差啊,搞一个简单的tab居然搞了5个小时才弄好, 其实官方的demo更加清楚,呵呵。     这边的a.class,b.class,c.class 中的a,b,c都是类


相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。