I am developping an app with an activity which switches between a few fragments. I was wondering if it is possible to have one of this fragment with tabs in the action bar, but not the other ones, knowing that, in general, the activity is tabbed and the tabs switch between fragments.
In a nutshell, I want an activity with a few fragments, and one of this fragments should have tabs to browse between other fragments, is that possible?
Ofcourse it is possible, take a look around StackOverflow, there are a couple of questions already.
Instead of normal TabHost your should use FragmentTabHost and because you will have fragments inside a fragment, you will have to use getChildFragmentManager() instead of getFragmentManager().
Adding tab inside fragment
Nice post Marko! I was just typing up basically the same answer. Here is a link for the documentation on Nested Fragments hope that helps as well
Related
I have made a swipe tabs using fragments. But is possible to use activities instead of fragments? For example. I have 3 swipetabs, "View","Add","Delete" and 3 seperate activities for each on of these functions. Is there a way that if a swap from "View" to "Add", it takes me to the "Add" activity, an so on.
As i don't have enough reputation i cannot comment on this so am answering. Instead of using activity try to use fragment as google suggests you can do everything in fragments same as you can do in activity infact you can do more thing in fragments so i recommend you to use fragments so please take a look at communication between two fragments https://developer.android.com/training/basics/fragments/communicating.html
In my requirements i need to add multiple nested fragments as am using navigation drawer as a main navigation so am using nested fragments like lets say Fragment a->b->c->d-> like that i have created is that a right way to do with using multiple fragments like would cost anything for application ? As beginner am very confused what to do with is it a bad practice i may be dumb question ! any solution you people are here to help beginner like us thanks in advance!
This is not a bad practice, but mostly we should keep fragments in Activity, instead nesting them which is complex but sometimes, it also depends on design of apps, which we must have to follow either by nested fragments or whatever.
In case of Navigation Drawer, you can be enough by adding fragment to navigation list items click and then start activity on fragment click.
Fragments are very useful.
An Activty can have multiple fragments ,and fragments cannot have fragments inside them.
In Android 4.2 there was support for Nested Fragments Where you can use fragment inside other fragment. But your fragment must be dynamic.
Thanks
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 know my questions sounds like a tongue twister. Anyway I wanted to implement it but I don't have any idea on how to do it. I already know on how to use a FragmentActivity and add the TabHost for it but now what I have is a fragment that should also contain a Tab which also has a fragment. To give it a more detail here's what I wanted to implement:
What I tried so far is to Create a new FragmentActivity which will be contained inside a FragmentActivity and I got a ClassCastException. Any ideas on how can I achieve this?
Using a newer version of the Support Library, or native Fragments if you only care about Honeycomb, you can add Fragments as children of other fragments.
You can call getChildFragmentmanager() inside one of your top level Fragments and add your child Fragments there.
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.