I'd like to show a different fragment in a TabHost, for every tab. But
it seems that there's no easy way to do this. TabHost can only accept
A. Views or B. Intents that launch Activities when the user selects a
tab.
Going with A means that I have to initialize every fragment and load
them into container Views that are given to the TabHost. But I want
these fragments to load only when needed - when the user selects their
tab that is.
Going with B means that I load the fragments into separate Activities
for each tab. But I'd like the fragment to be able to reach the
"original" parent Activity, not just some shell Activity that hosts
them in a tab content.
Am I missing something? Is there a way to manage fragments with
TabHost properly?
Some hits here:
fragment Support with Tabhost
Android 1.6 & Fragment & Tabhost
For what I understood, "link" a fragment inside a TabHost isn't allowed, you need to create activities that include your fragments and then call these activities from TabHost.
EDIT
the "official solution" from Google, and with nice animations.
Related
I have an App using ViewPager which has 3 tabs. I need each tab to contain a navigation stack of fragments e.g. I have a list on the first fragment which will then display a detail fragment based on clicking an item. What is the best way to design this? At the moment, I have one MainActivity which replaces the Fragments within each tab but as I'm adding more fragments within each tab, the MainActivity will just become huge. Can I handle all of this within the fragments themselves?
I think what you are looking for is a tabhost with backstack. This makes use of a TabHost, and is not the same as using a ViewPager with tabs. But that solution on Github is a very good one.
Also, this will not make your MainActivity "huge", because all the Fragments can be defined as separate classes in their own class files. Fragments are supposed to represent more modular UI blocks.
I was hoping someone could point me in the right direction when using fragments within a tab. I have a mutiple tab application. On one of the tab pages I have broken down the content into two separate fragments.
The issue is I do not want the fragment activity I using to control the tab to control also control the two fragments being displayed. I want an something between the two elements such as another fragmentactivity to control the fragments used as content.
Is this possible and if so how?
Tab Fragment
tab 1 tab2 tab 3
tab2 : display is two fragments
tab2 -> anther fragmentActivity -> two displayFragments
Transition to FragmentActivity
I believe I've found my answer. This can't be done. My fragments need to response to a fragmentActivity and the fragmentTransaction only transitions between fragments not activities. So they will have to respond to my activity controlling the tabs.
I have a FragmentActivity which hosts a Fragment which hosts a TabHost which need to set its tabs with Activities. Is that actually possible?
No, Activities cannot hold other activities within themselves.
For this, you use fragments.
However, you can use fragments withing fragments, even though I find it very confusing .
I am creating a tabbed application and the main activity for one of the tabs is a listing of departments. When you click on a department it starts a new activity that lists the documents in the department.
My issue is when it shows the document listing, the tabbed view goes away. And I would like to keep the tabs on the top constantly. I have attempted to extend the TabActivity to each of the activities that are started under the tab, but then the listviews create over eachother.
I have also attempted to create the tabs without a tablistener starting an activity, so I can only create the listener if it is the first time the tab is called, but that is not allowed.
Any help would be appreciated, maybe there is some method I am completely missing?
Thank you!
And I would like to keep the tabs on the top constantly.
Then you should not be starting an activity when the user taps on tab. Your tab contents should either be fragments or something else manipulated by your TabListener. The concept of activities being the contents of tabs is now deprecated (and, IMHO, was a bad idea to begin with).
I have attempted to extend the TabActivity to each of the activities that are started under the tab, but then the listviews create over eachother.
You will notice that TabActivity is deprecated, specifically for the activities-as-the-contents-of-tabs concept.
I use Tab Widget like in documentation example TabWidget
I have two tabs, and in every tab when I press some button I need to load another different activity. My question is how to achive that I have that two tab on another activities like headers ?
I could create that tabs in new activities but it wouldn't remember choosen activity for another tab. How to solve this ?
This is not possible out of the box. The normal TabHost doesn't provide any facility to change the content of a tab after it is created. If you can use views as your tab content, you can, however, flip between the views within a tab so that to get the effect you are after.
If you absolutely must use a new Activity, then you will have to implement your own TabHost which allows swapping tab content after it is created.