Android : TabActivity or Fragment? When to use Tabs, when Fragments? - android

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.

Related

What is the Android equivalent of a UITableView as used in this app I'm porting?

I'm trying to port an iOS app to Android. I'm an Android noob, but I'm quite experienced in iOS. Here is a screenshot of my app.
The UITableView has custom cell types: Switch cells, tap to toggle the switch on and off. Multi-value cells: tap to expand a UIPickerView. My iOS app also has a tab bar for more screens.
So I started with the default TabLayout template in Android Studio, and made it to show three different fragments.
I then moved on to making the main interface. That was when the PreferenceFragment popped up in my searches, and seemed like a very good idea – my iOS app has a relatively complicated architecture to keep track of the state, and the PreferenceFragment promised to take care of that transparently, so I was in.
However, the TabLayout does not seem to like PreferenceFragments because they are not the same as compat fragments (did I get that right?). Anyway, the linked question suggests a couple of solutions that I'd rather not follow (re-invent the wheel? Seriously?).
Reading around, people suggest I might get away with a PreferenceActivity instead, but I really liked the idea of fragments, mostly because I want my app to scale to tablet and have a "different" UI, with the two different lists of parameters showing up side by side – a job for fragments, right?
Now, the best I can figure is to go back to RecyclerViews, so my question is: is that it? And if so, do RecyclerViews support simultaneous different types of cells like UITableViews do? I should also say that I don't particularly care for Android <5.0, if that helps…
Any other hints or pointers for me to follow when porting iOS apps to Android?
The reason TabLayout does not seem to like a PreferenceFragment is because PreferenceFragment extends android.app.Fragment not android.support.v4.app.Fragment (you did get that right), which the TabLayout (in reality, the FragmentPagerAdapter) expects.
If you want the functionality of a PreferenceFragment to be paired with other app-compat widgets, import the v7 Preference Support Library:
compile 'com.android.support:preference-v7:24.2.1'
Then you can extend android.support.v7.preference.PreferenceFragmentCompat, which itself extends from android.support.v4.app.Fragment.

Proper usage for Activities in Android

From what I understand, and activity is equivalent to a "page" in a web app.
For example, the list view would be one activity, edit view another activity, and add view a third activity.
While this works as expected, android activities seem to operate as individual apps--the action bar is different for each activity and so are the menus.
Is my usage of activities above correct or should I be using some other mechanism such as swapping out layouts and views?
Fragments are a core part of activities - not that much different. The use of fragments comes since Honeycomb 3.0 and the idea is an option to split the screen in several fragments at once. For example if you look at the gmail app for a tablet - you have one fragment on the left dealing with navigation and then the next fragment on the right is the list of emails.
On a mobile device, viewing area is limited, so you it could be said that fragments sort of behave like an activity - you interact with one fragment, which triggers another and so on and so fort. But in the end you always reference a super activity of each of these fragments - for example when you want to access the context.
So if you just want to wrap web pages in WebViews, stick with activities. If your scenario might involve developing for both tablets and phones, then go for the fragments.
Alternatively, you can read about the design philosophies of both here:
http://developer.android.com/guide/components/fragments.html
Good luck!
Fragments are to be used when you have common feature across multiple activities. From my perspective you should use individual activities as there will be back-end code to support (fetch and validate data, i.e. business logic). This way you have more modular code. Fragments is a new feature from v3.0.
From what I know, Fragments would be a good option to be able use different configurations/real estates on different devices. For example, if you are on a device with big real estate like Tablets or TVs you can display more content in a single activity, on the other hand for a devices with smaller real estate you can show content based on a progressive manner.
See this: http://developer.android.com/training/multiscreen/adaptui.html
Note that Fragments are supported only on devices running Android 3.0, so you might have to use Support Fragments (See: https://stackoverflow.com/a/6528757/713778)
But Again it depends on your specific needs. I am not sure what your exact use case is, but I would suggest you to watch some Android design in Action for some examples to improve your design and make your app "User Centric" (See: https://www.youtube.com/playlist?list=PLWz5rJ2EKKc8j2B95zGMb8muZvrIy-wcF)
I recently came across this https://www.youtube.com/playlist?list=PLWz5rJ2EKKc-riD21lnOjVYBqSkNII3_k seems provide an in-depth analysis on User Experience.
I would recommend using fragment for web like views. Fragments are pretty snappy compared to activities and reusable.
But before you start, make sure to fragments are good fit for your requirements since they are supported from android 3.0.
You can declare fragments from xml itself or you can create frame layout and add that view in the code itself.
http://www.c-sharpcorner.com/UploadFile/2fd686/fragments/
The above link has a good example tabhost.

load different activities in tabs

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.

Android Tab Bar Controller (Fragment or Action Bar or TabHost)

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.

Correct Way To Implement A Tabbed Bar

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.

Categories

Resources