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.
Related
I want to implement a Navigation Drawer in my app but I am conflicted on whether I should use it with Fragments or with Activities (see image below for more details).
Is there any real advantages or disadvantages between the two or is it just a matter of preference?
Edit:
Just to clarify my question:
In the case of using Activities instead of Fragments;
When I select "Import" that will open an Activity and not a Fragment and if I select "Gallery" it will open an Activity with contents for gallery item etc. and so on for the other items in the Drawer window.
In the case of using Fragments instead of Activities;
If I choose from any of the Items in the Drawer window it will open their contents in Fragments for each Item selected instead of starting new Activities for each selction.
Remember Fragments need an Activity. You always have one minimum when using Fragments.
If you are talking about to use like main element in the most cases is best use fragments because you have more flexibility UI.
The performance would be better if you have 3 activities and 10 Fragments or have 13 Activities? Think about it, the navigation within the App would be the big challenge but it's just about using the right flow in your application.
Edit:
For instance:
Drawer With Activities instead of Fragments
If you were to use NavigationDrawer without Fragments then it would be best to keep the NavigationDrawer instance in a single Activity and when you navigate the app by choosing from the items in the NavigationDrawer then each of those Activities that are started should not implement the NavigationDrawer but instead should implement the back button to Navigate back to the "Main"/single Activity that the NavigationDrawer was implemented in.
Note: If you do want to implement the NavigationDrawer in multiple Activities you would have to recreate a new instance of the NavigationDrawer in every Activity that you desire to display it.
I suppose this would be a disadvantage vs using Fragments whereas if you used a fragment you wouldn't need many instances of the drawer you would only need one.
Drawer With Fragments instead of Activities
If you use the NavigationDrawer with Fragments then the drawer should be implemented in a single Activity and when each drawer item is selected, their contents are displayed in each of their very own Fragments(which is called inside of the central Activity which manages all the Fragment instances)
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
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'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.
My App (minimum API 14) uses an ActionBar with 3 tabs.
The tabs are:
i. enter data (approx 10 fragments)
ii. manage data (15 fragments)
iii. view data (8 fragments).
Each tab has a default fragment, but then has multiple fragments depending on user choices.
I would like the user to be able to swipe between the 3 tabs (by swiping the content) hence I need to use ViewPager and the compatibility library v4.
The advice I have picked up (after much research) is to use a MainActivity which hosts the ActionBar and tabs with fragments for the tab contents. But I'm worried about the number of fragments. Also some of the fragments need to use date and time pickers which means DialogFragments coming out of fragments. It's starting to get very complicated.
Does anyone see any problem with my using Activities instead of fragments for the tab contents?
Fragments act on the UI side just like activities except you can combine multiple together. So you don't need to create a different fragment based on user choices, you just need to change the fragment just like how you would change an activity dynamically.
Also if you want to use ViewPager, you are basically forced into using fragments.
The nice thing about fragments is that they are reusable, so most likely you only need a few fragments and then you can combine them in different ways for the different use cases. This also makes your tablet UI a lot easier to create.
For reference: http://developer.android.com/guide/components/fragments.html