This question is a bit hard to word, I will try my best.
I have this android app that uses a navigation drawer, so within the main_activity, the navigation drawer would change the different kinds of fragments that I want.
Within one fragment, I want it to be able to have four tabs. The functionality of the tabs is very simple as it basically just display some pictures and data. However, I don't think android allows you to have a tabhost within a fragment.
I can achieve a similar result by creating my own buttons at the top and it dynamically changes the View that is within this fragment, however, I am wondering is there a more intuitive way of achievement the same result?
You can use TabHost inside a Fragment just fine. A TabActivity is not necessary to host the tabs. TabActivity only provides some functionality that you can also add to a Fragment yourself. Check out the TabHost docs, especially the section about addTab(). You can also search here on SO and you will find very helpful posts like this one.
Related
I am coming from iOS and working to port an iOS app on android. In iOS there is the storyboard where you can connect different view (activities) and embed them into tab bars and navigation bars. I am having an issue to understand what is the best way to implement bottom navigation in android...
Let say I have 3 bottom navigation items with the following
tab1
page1.1
page1.2
tab2
page2.1
page2.2
page2.3
tab3
page3.1
page3.2
page3.3
As an example in tab1 I have page1.1. Then let say I have a button in page1.1 which bring me in page1.2. If I go back from page1.2 I want to go back to page1.1. Same story for page2.1, I can go to page 2.2 and from page2.2 I can go to page2.3. I also want to go from page3.2 to page 1.2
I have read fragments are the best way to do it (each page.x is to become a different fragment with a different layout) but it does not seem very easy. I have also read I can use activities but many suggest not to use it
What do you recommend that I focus on? Are there any other solution to consider on top of fragment and activities? Thanks
You should use Fragments with a Single Activity. If you don't, you will have to copy/include one bottom bar in multiple Activities. It might be useful in some cases however when Activity is switched, your bar will redraw which might turn out to be a bad user experience.
I have read fragments are the best way to do it (each page.x is to become a different fragment with a different layout) but it does not seem very easy
Using Fragments might actually make it easier than if you use Activities. If you use Fragments, you can have one Activity be in control of every fragment that is being displayed, meaning that you can control navigation based on which fragment is being shown. This way, you can handle some special scenarios that do not fall into usual navigation behavior.
Doing the same in Activities would be a little more difficult since you'd have to continuously pass around data in Intents and it would be difficult to control the behavior since your logic will be spread across Activities.
What do you recommend that I focus on? Are there any other solution to consider on top of fragment and activities?
These two are only recommended solutions. If some of your UI elements are same in all pages while some part of it is changed constantly, then its best to use Fragments with single Activity. For screens that are completely different or that are not part of your navigation flow, you add more Activities for them instead of Fragments.
For example, You can use Fragments in One Activity with Bottom Navigation Bar, but for settings screen or profile screen, you should make separate Activites.
I have a navigation drawer activity. I want to create multiple activities like the same dynamically as per user data.But I guess that we cannot create entirely new activities on run-time. So, is there a way to convert my activity(navigation drawer activity) into fragment and use it? If it is possible, how can I do it?
From You Question what I understand is that you want to create an application with an Activity having navigation drawer and different navigation items are fragments which are contained inside the activity.
You can do that easily and that is better that creating multiple activities.
read the following link and follow along same lines for doing that.
https://github.com/codepath/android_guides/wiki/Fragment-Navigation-Drawer
I have 2 Activities. One of it is displaying line chart, another is displaying pie chart. I would like to embed both in a single launched Activity. User is allowed to perform single click, to switch between line chart and pie chart.
I realize ActionBar's navigation tab, or drop-down navigation is the best candidate to help me achieve this purpose.
From Android API demo, and guideline found from http://developer.android.com/guide/topics/ui/actionbar.html, I realize all switched components are implemented as Fragment, not Activity
Does it mean that I need to port my previous 2 Activities into 2 Fragments, in order to embed them into ActionBar's tab navigation view/ drop-down navigation view?
Is there any other ways I can do without porting? But, is it advisable as I do not find an official example by using Activity.
In API demo, I realize most Fragment is implemented in the following pattern.
public class FragmentStack extends Activity {
...
public static class CountingFragment extends Fragment {
// CountingFragment never access any members in FragmentStack
}
}
Is there any reason to do so? Why don't they have CountingFragment is a separated file?
If you want to embed in a single activity, the preferred way is to use fragments. You could probably also use custom views, but Fragments have a well defined lifecycle and handle a lot thigs for you. So it might be a good idea to extract functionality into fragments, and have the activities only as shells, embedding the fragments (if you still need this with your new design). You can switch between fragments any way you like: with buttons, dropdowns or any other UI that fits your app; you don't necessarily have to use the action bar. As for 3. there is nothing wrong with defining fragments in separate files. Activity and fragment are in the same file in samples mostly to make it easier to follow the sample.
I am making my first android application with the ActionBarSherlock.
The application will always have an action bar consisting of 3 tabs (first tab selected by default).
The app could be extended for use with a tablet.
I have been searching the web, and following the android development guides, however I am finding a few things confusing.
The first tab screen will be a list view with a list of items, onitemselected should send the user to a screen which features more details about that item.
When should I use a fragment? Should each tab be a fragment?
Or, should each tab call a new activity, which consists of fragments?
And, if using fragments, should I place them in different classes, or embed them within an activity??
Appreciate any help, thanks.
you should probably read these two links first.
http://android-developers.blogspot.com/2011/09/preparing-for-handsets.html
http://android-developers.blogspot.com/2011/02/android-30-fragments-api.html
If you plan to make an app that work on both phone and tablet. It is a good idea to use a fragment, and then use a shell activity to wrap that fragment.
My experience with Fragments is mostly on ViewPager, so I am not entirely sure if it applies here.
In Android, you should use Fragments as much as possible. As a general rule of thumb, imagine you are translating the UI from phones to tablets, elements that can stay together in the same configuration should be a Fragment.
There is a Fragment subclass called ListFragment, so you might want to look into that for your first tab. (ListFragment is for Fragment like ListActivity is for Activity)
There is also a tutorial I found to deal with fragments. Did not really look into it but I hope it helps.
http://android.codeandmagic.org/2011/07/android-tabs-with-fragments/
As for ActionBar / ActionBarSherlock, I have absolutely no experience withit so someone might want to add to that.
I'm currently creating an app in which the main screen is build up out of 2 Fragments.
When the user selects options on the main screen, one part of the screen gets replaced by a new Fragment, all pretty much basic stuff.
Now I'm trying to create a screen with several tabs, which all open a new fragment inside them. I had this working with regular intents, but that was before switching to Fragments.
I read that this is possible by using a FragmentActivity, but sadly you can't replace a Fragment with a FragmentActivity, simply because the transaction won't let you.
Is there any way of doing this inside an ordinary Fragment? Or should I try mimicking the behavior by using a layout with a fragment inside which gets replaced by another one at the press of a button, much like the main screen? (Or won't that work due to fragments in fragments?)
There is an example in Android's support library that describes what seems to be what you need. You can find it here: FragmentTabs.