It took quite a while searching stackoverflow and google and can not find a guide to properly explain to me how to implement Tabs in my application.
I know it should not be used TabActivity, I also read about the recommendation not to use TabHost (at least that understanding). My interest is not just changing the content is run a full activity when changing tab.
Example
MainActivity:
- Tab1Activity
- Tab2Activity
- Tab3Activity
My goal is to build an application that is supported by a wide range of devices from api 8 to the latest. I'm using android-support-v4.jar.
My second goal is to load in each tab an activity because at least two of them consume enough resources and speed should always prevail.
Can help me with a guide or explain a "correct" way to achieve this.
You should use Tabs with Fragments.
Look this : http://android.codeandmagic.org/2011/07/android-tabs-with-fragments/
As the other suggested, the best way to implement tabs in your application is to use fragments.
You should not use activities contained in a tabactivity, which is currently deprecated. Even the official documentation suggests to use fragments, stating that
New applications should use Fragments instead of this class
However, if you still feel retro' and you REALLY want to use tab contained activities, you need to use tabhost. A nice tutorial can be found here, but again, I strongly recommend to go for the fragment way.
Related
I want to create an application which one of its pages must like below :
Each tab contains some Views ( like a form )
After some search, I know there is two ways for implementing above layout. TabActivity and Fragment. But I have not use neither Tab nor Fragment in my projects(cause i have short experience on Android)
Now which of these ways I should use? Clearly, I want to know which is more suitable to use? (According to the better one, if there is a brief and clear tutorial, please introduce it to me)
Note : In this case, the application must run on Android 2.3.3+
TabActivity is deprecated. You shouldn't use it, especially if it's an entirely new project.
Fragments are available for older SDKs via the Support Library, so you can use them in 2.3.3+ with no problems.
I was able to get a TabActivity working properly with multiple Activity's using ActivityGroup. However, I noticed a weird behavior. When clicking on multiple screens in one tab, going to another tab and navigating back to the original tab where user was already navigating several screens, the ActivityGroup goes back to the main Activity rather than the last visited one.
I was following the tutorial here:
http://ericharlow.blogspot.com/2010/09/experience-multiple-android-activities.html
Is there a work around for the ActivityGroup to prevent this kind of behavior?
I think it is better to look for a tutorial on Fragments.
I know this is not really the answer to your question, but I think it would be better to immediately use Fragments. This because Fragments are now recommended for Android development, and should be used as much as possible in favor of older technologies. And the stuff you want to do, are the perfect example of things that should now be done with Fragments.
The tutorial you are pointing to is from 2010, so that means that the technology used there will be rather old (I haven't read it though, but I can imagine it will). It is best if you want to develop something, to immediately use the latest technology, in this case: Fragments.
Some information on Fragments
I've been slowly porting my iOS App to Android so I can take my time and make sure that it functions correctly and I'm now at the part where I need to create a TabBarController like environment. I've been searching for days and have come across numerous examples but with each example there's a problem.
First, most examples refer to using a TabActivity, which when I just checked is deprecated and can't be used. The one's that don't use TabActivity either use Fragments (sometimes with an Action Bar) or a Tab Host. It looks like Fragments is the way to go, but then how to you support older devices (from the Android website over 50% of there users are still on API 10).
Basically what I need to set up is a Tabbed environment that will let each tab load a new Activity, and then each of those Activities will have their own navigation based hierarchy as well so I'm trying to see what is the best option to pursue and if anyone has seen some examples that can help.
I've found 4 tutorials so far that have fragments and different activities for each Tab, so I'm going to look into those now but just wanted to see if any other developers that have already come across and issue like this did to work with the compatibility issues with older devices.
You can use Fragments all the way down to API 4 using the Support Library. I'd use a single Activity with multiple Fragments in ActionBar tabs, though you might need to do it with TabHost to support lower APIs.
I'm having serious problems getting three layers of nested tabs to work in an app that runs from Android 2.1 up and looks like Android 4 (uses support library fragments).
The goal
App should have an ActionBar (works, currenly uses ActionBarSherlock)
3 fixed tabs on the main screen, that don't move into the ActionBar even if the screen is large enough. The second of these tabs contains...
About 4 tabs that were loaded from a server when the user logged in the first time. Each of these contains
About 10 swipable tabs (like in the Play Store) that were loaded from a server when the user logged in the first time. My idea here is to use ViewPagerIndicator, since that library is written by the same guy who ActionBarSherlock which should keep problems down to a minimum. But I'm open to ideas here). Each of these swipable tabs contains something that is currently a Fragment, but could be changed.
The Problem
When this was an Android 2 app, it simply used nested TabActivities, but these don't work with all the Android 4 stuff. I don't understand how to do this probably, especially the "you can't nest fragments" restriction is causing me headaches. Also, it seems that you can only use one FragmentManager per Activity, so my idea to have one in each of the second row tabs didn't work (All except for the first tab remained empty).
How to do this the right way?
(Please understand that "Use a different GUI design" is not an option since this is what the customer asked for and he won't reconsider)
PagerAdapter does not require Fragments as children. You can inflate/manage your own custom views in there. So you could continue to use nested TabActivities. Or, you could put Fragments at the top-level, and manage your own Views in the bottom-level ViewPager.
You could also, theoretically, use ViewFlipper if you want to keep the Fragments in the ViewPager. You'll have to write your own LinearLayout with Buttons as tabs, but this is easy. If you're looking for the Holo look, simply set the style to the ones found in ABS.
Another option is to use TabHost without using the TabActivity. You can even use it with Fragments. See the example here.
Also note: If you're looking for the Google Play style of ViewPagerIndicator, thats now inlcuded in the Support package: PagerTitleStrip.
I'd imagine that your best option is to use Fragments as the top-level, as this will help with memory consumption.
That said,
I must say that this sounds like a terrible UI pattern. Even worse, we are talking about a lot of inflated views in one Activity. You may run into memory issues here, depending on whats being shown. I suggest heavy usage of ViewStubs and recycling if you keep the ViewPager at the bottom.
Keep trying to push the client toward using the ActionBar spinner pattern for top-level (main 3 tabs), or even consider the fancy sliding drawer pattern. Perhaps that smooth animation could be enough to sway their opinion.
Refer them to the official design website, and show examples of popular navigation patterns like the ones found in Gmail, YouTube, Google+, Evernote, etc. I recently dealt with a client requesting the exact-same pattern you describe, and after weeks of pushing was able to convince them that more-than` 2 layers of tabs is unacceptable.
You can also show them my Wall of Shame Google+ page, highlighting bad design patterns used in popular Android apps: Android UI Anti-Patterns. :-)
I have been browsing the web for a while now and looking at different examples of how to implement a tab bar.
The problem I am having is deciding the correct approach to creating this feature. The option that seems most used is to have one main activity called MainActivity or something and using fragments as the tabs. This is all well and good and I have created an application like this fine.
The problem I am having is I am in two minds as to whether this is the right way to go about it as coming from an iOS development background it just doesn't seem right due to these tabs having different functions.
Any pointers would be great.
EDIT I have also looked into ActionBarSherlock but couldn't get to use it in a project as it kept coming up with errors and I couldn't find a thread that sorted the problems I was having out.
Disco
You should be using fragments, yes. There is an example of how to do it here: http://developer.android.com/guide/topics/ui/actionbar.html#Tabs
This "only" works with Tabs that are part of your Actionbar, though. Shouldn't be too much of a problem, because they usually should be part of your Actionbar. You can also manually move the tabs if you're not content with their location, or roll your own buttons that look like tabs.
Keep in mind that tabs in Android aren't quite the same beast as the tab bar is in iOS. They look similar and behave similar, but there are subtle conceptual differences in how they are supposed to be used. Don't use them for your basic app navigation, but rather for switching between similar "things".
If you want to employ this on device pre-3.0 though, you will have to use ActionBarSherlock. It does work fine and we're using it in several projects, so if you're having problems integrating it I suggest creating a new question for that, I'll be happy to help.