I'm thinking of adding tabs to my android app. Currently, I am using the same activity and adding the appropriate fragments based on the tabs selected. I was wondering if that's a good way, or should I add an activity per tab and then within the individual activity, have fragments as needed?
I think you are on right track. Have one Fragment Activity and attach all the fragments to that activity.
Related
I am new to Android programming. I started building an app using YouTube tutorials but I am a little bit confused and facing a problem. Should I use fragments or activity in my sliding tab? I am working on an Android project of employee attendance and payroll so I am thinking of using a slide tab (something like the screenshot below). The 1st tab may contain a form to add an employee and after adding it, will display information in list view and the other may contain salary. Should I try fragment or activity for my tabs?
A fragment is always located in an activity.
So you wil always need an Activity and then add the fragment to the activity.
if you say you want to slide?
you probably mean a viewpager, that can have n fragments. the viewpager needs a special viewpageradapter. where you define how many fragments you want. and at which index what fragment needs to be shown
I want to divide my screen into four parts and add activities in each part. I am not interested in using fragment. Each activity should behave independent of other. Attached photo is showing what I exactly want to do.
In each child activity I want to add VideoView or WebView depending on the selection from menu item.
How can I do it. I didn't find any way to add activity to an activity.
Thanks :)
PS: Activity means Activity not fragment.
i strongly recommend 'using fragments' in your case as ActivityGroup is deprecated in API 13..
The only way you can do it by using fragments. Here is the simple example on how to add multiple fragments on single activity Link
Edit link
You can achieve this design by using four fragments, one for each child view inside a single activity.
The recommended way is to create a single XML layout for your activity, and create four fragments inside that layout.
What scenarios using fragments what scenario using the activity , How can I make sure you when to use the activity when using fragments !
Basically, when you want a fixed portion of the screen and the rest will be changing, you want to use fragments.
If all your app is going to be on different screens, you want to use Activities.
Anyway, at least you will need one activity to hold your fragments.
Hope this helps.
As i know,You need to extend the fragment when you are displaying the view into tabs.
Otherwise you can extend activity.
It depends on your requirement. if u want to expose your toolbar throughout your application you can use fragment both activity and fragment are quite similar . You can also use only one activity remaining things may be your fragment
It depends on UI which you are creating.
If you are creating Multi-Pane UI then you should use Fragment.
And if you are creating some part of screen which can be reused then for that part you should create Fragment as Fragment is meant for re-usability.
If you are creating stand-alone screen then you should use Activity
For more info please refer this link How to choose Activity or Fragment if both scenarios are possible?
I have an app with 4 main tab fragments. However, inside each tab, the user can do different actions which brings him to navigate inside that same tab. Should I launch new activities or just replace the tab fragment with new fragments? The big downside I see of using new activities is I lose the tab navigation view.
What should I do? Use only fragments? That's a lot of fragments for one activity.
Use fragments. They fit your situation perfectly. And don't worry, 4 fragments inside one activity is not too much. Fragments are designed to be lightweight, so you can use even more.
I'm trying to create a layout that has a stationary footer with activities that slide behind it. I've been told to use Fragments, but that would mean that I would have to convert my already existing Activities to Fragments - right? Here is a diagram of what I'm trying to achieve: http://i.imgur.com/K8Iao.jpg
What I think #TarunMaheshwari is trying to say is that instead of having 3 activities (eg. classes with extends activity), replace it with extends fragment (obviously there are other minor changes you might have to make for the code to work) and then create a main activity (with extends FragmentActivity) that has the static footer you want which can call on the 3 different fragments.
Recommended readings:
http://developer.android.com/guide/topics/fundamentals/fragments.html
http://android-developers.blogspot.ca/2011/02/android-30-fragments-api.html
I believe using fragments is the right solution for your app. However, from what I understand from your question and comments, you really want to avoid using them. To use activities instead of fragments, implement a Tab Layout with a Tab Host and Tab Widget as explained in this tutorial. This solution allows you to use the tabs to switch between activities.
To align the Tab Host to the bottom of the screen, have a look at this tutorial.