How to support tabs on Android 2.2+? - android

I'm trying to offer tab support for all Android versions 2.2 and above. Is there a way of achieving this without using any deprecated classes/methods?

The Problem is as following:
TabActivity is deprecated because it derives from ActivityGroup which is deprecated, too. Problems are, to start Activitys inside Tabs and don't break callbacks like onActivityResume. There are some 'Hacks' around which solve this but they are ALL relying on the deprecated LocalActivityManager. So I see no solution of using Tabs without ANY deprecated calls. Since Android 3.X google invented Fragments for this kind of things which are supported on lower Versions with the SupportPackage. You should definetly try them in Combination with the new ActionBar if you want to avoid deprecated classes/methods
EDIT:
Link to FragmentTabs for further reference

I recon with with the answer Rafael gave.
Instead of resorting to the TabActivity you can sill use the regular TabHost to show tabs in all versions of android.

Related

How do I use tabhost?

I tried to use almost all examples and source code from internet and all of them just doesn't work. I got this warning "The type TabActivity is deprecated", anyone have source code for Tabhost that is really working and doesn't crash?
You should not be using Tabs anymore. Look after Fragments instead. a good place to start is
http://developer.android.com/guide/components/fragments.html
From the documentation for TabActivity:
This class was deprecated in API level 13.
New applications should use Fragments instead of this class; to continue to run on older devices, you can use the v4 support library which provides a version of the Fragment API that is compatible down to DONUT.
You should learn how to use Fragments instead. TabHost/TabActivity isn't being actively supported any more, so if you're just learning how to do tabs, you should do it the new way. No point in learning something, only to have to replace it anyway.
From the link here:
This class was deprecated in API level 13.
New applications should use Fragments instead of this class; to continue to run on older devices, you can use the v4 support library which provides a version of the Fragment API that is compatible down to DONUT.
So the suggestion is obvious - use Fragments. But don't ask here how, create a new Question.
go through the link:http://developer.android.com/training/backward-compatible-ui/abstracting.html Download the Sample App: TabCompat.zip

"The type TabActivity is deprecated" For app tab

"The type TabActivity is deprecated"?
I am making the Tabs of app following tutorial book.
I've checked from the android developer.com website, but i have no ideas on the significance of the following message : This class is deprecated.
New applications should use Fragments instead of this class; to continue to run on older devices, you can use the v4 support library which provides a version of the Fragment API that is compatible down to DONUT."* (http://developer.android.com/reference/android/app/TabActivity.html)
What is v4 support library?
How to finish the tab functions?
You can still use a deprecated package. It is however recommended to use Fragments, and thus the support package. You can read more about it here. However, if you are a beginner at java and android development, I would recommend ignoring the deprecation for now and come back to this when you have completed the tutorial you are currently using if you find it educating.
If you want to watch a nice example of tabbed navigation using Fragments, then create a new project in Eclipse using android 4.0 or later. Make sure your android-plugin is updated. You will get the option to create a project with basic navigation already implemented.
"Deprecated" means that the api developers don't recommend using it anymore, probably because its not a good model, or inefficient, etc. Fragments were introduced in Honeycomb and can be used to provide a similar functionality as tabs and is more in-line with android's current design philosophy.
Since Fragment was introduced in Android 3.0 Honeycomb, you might think you cannot use that for pre-Honeycomb devices. Enter Support Libraries. They are libraries which you can include in your application which needs to run on pre-Honeycomb and still use this class.
So if you want to, you can finish the TabActivity as described in whatever tutorial you are following, it'll probably work on a few more upcoming android versions. But it is recommended that you start using Fragments.

Why is TabActivity deprecated (reason)?

This is not a duplicate of How can I use fragments, now that TabActivity is deprecated?
After some research the conclussion is that TabActivity is deprecated because we now have to use fragments. And the reason for that, as far I read, is that fragments work better with the action bar, and with the support compatibility library it's also possible to implement starting at Android 2.1
But I still don't really understand the reason why it's deprecated. Why not just make that the action bar also works with activities? Why are fragments prefered?
I also read fragments have better performance... is that the reason?
Activities at least are cleary separated entities which is something positive. I don't have enough insight in fragments now to understand why they are better.
Thanks in advance...
TabActivity has been deprecated, cause it is subclass of ActivityGroup, which also has been deprecated.
ActityGroup has been deprecated, and instead Fragment has been introduced and suggested. As using Fragments is easier and more flexible than ActivityGroup. It also enable android components to have a homogeneous pattern.
The main reason for which Google deprecate some methods / objects is to enforce best-practices and get the most homogeneous patterns accross applications.
Marking TabActivities as deprecated will make developpers use the ActionBar system for new applications, but you still can use TabActivities, although it's not encouraged.
Sometimes, They will mark something as deprecated for performance or because the behavior is not up to date with the latest API (for example the Clipboard system, changing from a "text only clipboard" to a "copy and paste anything".

Is it ok to use TabActivity still for my app?

My app is in need of some tabs and the TabActivity works great for it. It's much easier than wrapping my head around those Fragments... but I see that Android says to do Tabs in FragmentActivity now... My question is If I'm targetting 2.2 phones is it ok for me to use the TabActivity instead? Will I run into any issues in the future by making the app this way?
Its recommended to use Fragments, i know it has an initial learning of few hours but its worth the effort. Use compatibility library for using fragments in Android versions lower than 11.

On Android's Support Package, use it or not?

I've been thinking about the pros and cons of using Android's Support Package when targeting the latest API and setting the min SDK to 7, for example.
The Android documentation states "The goal is to simplify your development by offering more APIs that you can bundle with your application so you can worry less about platform versions"; however, I'm having some doubt on whether it will make it simpler.
Consider the TabActivity, which has been deprecated. The alternative to using TabActivity is through Fragments and by looking at the example to get a tabular view working, it doesn't look simpler. Besides, I have to use reflection anyways when deciding on to use the Fragments class versus the FragmentActivity class, so why not just use TabActivity. I was hoping to get your opinion on this. I'm leaning towards not using it, but I would like to know if I'm missing out on any benefits.
The alternative to using TabActivity is through Fragments
An alternative to using TabActivity is through Fragments, using the icky stuff in the example you cite. The better alternative to using TabActivity is to put your tabs in the action bar, perhaps using ActionBarSherlock's fork of the Android support library that offers a backwards-compatible action bar.
by looking at the example to get a tabular view working, it doesn't look simpler
It's not.
Besides, I have to use reflection anyways when deciding on to use the Fragments class versus the FragmentActivity class
If you are using the Android support library, you should not need this, as you always extend FragmentActivity.
I'm leaning towards not using it, but I would like to know if I'm missing out on any benefits.
If you plan on supporting tablets and/or TVs, you want to be using fragments. Fragments are useful even in phone-only apps, but not quite as visibly.

Categories

Resources