6

As mentioned here, and some other places that it is better to use fragment instead of activities with bottom navigation

If it is not recommended, then i have few questions,

I will have 5 items in bottom navigation, all of the items will retrieve data from web services, and have large amount of data, each will have to do parsing and have complex layouts.

Now my question is can an activity with these type of fragments can create any impact on performance.

and why it is not recommended to use activities with bottom navigation.

Taha Kirmani
  • 189
  • 2
  • 11

1 Answers1

11

If I was in your position, i'd use a view pager in combination with the bottom navigation component. This will manage the creation/deletion of fragments and should address your concerns with performance. It will also preload the previous/next screens for the user and can make the network calls on those screens before the use even goes to them.

Here is a pretty simple link showing this being done: http://www.truiton.com/2017/01/android-bottom-navigation-bar-example/

To address your question why not to use activities, this kind of widget and situation is exactly what fragments were created for. They can easily be nested inside of an activity and swapped between. Activities weren't created to be nested inside one another. Instead you'd have to have a bottom navigation view as part of each of the activities, and manage the switching for each of them. You'd lose the animation of swapping between them. Not only would this create a lot of unnecessary and duplicate code, but activities are heavier and you lose the nesting aspect.

This is exactly what fragments are meant for. I've followed this paradigm on several applications that sound similar to yours, network requests on each screen, fairly complex layouts on each tab. This is a pretty standard way to go about it. Hopefully this helps, let me know if you need a further explanation.

Kyle
  • 1,430
  • 1
  • 11
  • 34
  • Thanks @Kyle, I think view pager approach will take more loading time, as it will be loading data for 2 fragments at one time. What if i make a base activity class that have the bottom navigation functionality, and each activity class extend that class. I have seen some example regarding this approach. – Taha Kirmani May 01 '18 at 18:27
  • That is an option, you'll just be maintaining a bit more code as you're maintaining the navigation state between X activities, even if they all extend the same base. It is definitely possible, I'm just having a hard time thinking about when it would be preferred. The cool thing about the view pager and fragments is each fragment can be loaded on its own thread. So your network calls with all be on separate threads. Pages will be loaded simultaneously. I'll usually have a progress spinner on each fragment where it makes sense that is shown and hidden based on its specific network calls. – Kyle May 01 '18 at 18:45
  • The user can swipe between or switch tabs while the fragments are loading. So maybe one takes a bit longer then the other, they can go to the next one and the previous one will still be loading in the background. You know what you need the code to do, so maybe there is a reason the activities are necessary. The advice here is just based on my experience with applications. Good luck with which route you choose! – Kyle May 01 '18 at 18:47
  • One more quick follow up. Even if you don't want to use a view pager, you can still follow this paradigm with fragments. You'll still have 1 activity that contains the bottom navigation. And then have a FrameLayout as a container that takes up the rest of the screen. When users switch tabs, just replace the view inside the frame with the fragment of your choosing. This way you only load one at a time like you want. Fragments have a life cycle themselves and you should be able to do everything you need to do with this approach as well. – Kyle May 01 '18 at 19:32
  • Thanks @Kyle,I may be following the second approach, but the only thing i am concerned about it that, there will be view with in views as well, for example, if you tap on Players from bottom navigation, then there will be a list of items in there, let say player will have `PlayerNews`, `PlayerStats`, `PlayVideos`, these all are separate activities, if i go with `fragment` approach, then in my whole app there will be just 1 activity and all other items will be `fragment` right? Then is it the right or recompensed approach to have just 1 activity in the app and each other items is `fragment` – Taha Kirmani May 02 '18 at 09:37
  • Hey Np. So you can still use activities for that kind of navigation if you want. This is how I would think about it: Your first activity has 4 fragments that use bottom tabs. These are all on the same level in the hierarchy. You are using horizontal navigation for these. However, each one of these can have its own vertical navigation. So for instance, your 4th bottom tab is: Players. On this fragment, you have 4 buttons (PlayerNews, PlayerStats, PlayerVideos). When you click on any of these buttons you can launch a new activity. So now you are using vertical navigation. – Kyle May 02 '18 at 15:22
  • This vertical navigation will no longer have the bottom tab bar. You are now outside of the original main container and into the player info. When you hit the back button or up navigation, you can go back to your tab container with Player still selected. Does that make sense? With that being said, you could definitely do the whole thing with fragments if you want, but it could get tricky as you'll be have to manage the fragment back stack for each of your tab fragments. If it was me, i'd probably use container activity, fragments for bottom tab. then launch activity from fragments. – Kyle May 02 '18 at 15:25