4

I have an App which has, let say 15 different sections. I use a single Activity with a FrameLayout where I load 15 different Fragments using a NavigationDrawer. I also have child fragments under few parent Fragments where I load swipe Tab layout.

So, in short, I have lots of fragments in my App.

The issue is, if I add the fragments to the BackStack while adding to the FrameLayout, the fragments never gets destroyed as long as my App lives (only View gets destroyed, which is expected behavior by design). To make it worse, if the user keep hovering to different Fragments, the size of the BackStack keeps increasing which can result memory issues.

So, I started googling and found several threads in SO where some suggests against using Fragments at all. But if I want to design each section using its own Activity, I have to add NavDrawer to each activity (or at least extend those activity to a base activity) which I am not sure if prudent.

That leaves to the question, is it a good design to have lots of fragments in a single activity? If it's fine, should I add the fragments to the BackStack? If I should, what about memory issues? And finally, has anybody tried NavigationDrawer across different activities? Is it efficient?

My apology for a series of questions.

Edit: Based on the responses so far, I want to clarify that, I know it is sort of generic question, and can result different opinion-based responses. So, I want to make it clear that, I am not looking for any decisive answer (becuase there might not be any), rather I wanted to open a discussion to hear from different perspectives.

Community
  • 1
  • 1
abdfahim
  • 2,523
  • 10
  • 35
  • 71
  • 1
    This question will only serve to receive opinion based responses - You mentioned opposing opinions already! Do you think that using Activities will be more efficient? Remember Activities have an Activity stack as well. Only you can answer your own question by testing both and analysing performance and memory usage. The amount of Fragments isn't the issue, how you interact with them from a navigation perspective and what they contain is ... images etc .. – Mark Jan 12 '17 at 01:02
  • Hmm, I agree .. I know I asked a generic question. Actually, i was exactly hoping to get the different opinion based responses, to cross check my understandings with more experience coders. I am not actually looking for a decisive answer (because there is none, as you said). Of course, I opted to Fragments after doing my research, but just opened this discussion to hear contradictory views. – abdfahim Jan 12 '17 at 01:27

1 Answers1

1

This is a very general question.

Using Fragments is a well tested Android pattern. They provide you with a handy, small scale View Controller with a fully managed lifecycle. That's neat.

But Fragments are not always the right tool for the job.

My rule of thumb is: I use Fragments for whenever I need to manage a complex View's lifecycle, for Fragment animations and whenever I need an Activity which is not full screen.

If you find yourself having a lot of Fragments, then ask yourself what can you abstract or generalise. For instance: most list Fragments look and perform the same, and each row should probably not be implemented in a Fragment of its own.

The backstack is, IMHO, a UX tool. It needs to be managed in a way that makes sense in your app's business logic. It's a tool for allowing your user to naturally navigate your app. So pushing more than 4 or 5 Fragments in the stack dos not make sense, as you should not expect the user to remember 5 navigational decisions by heart. If you find yourself in a large backstack situation, you probably need to rethink your UX design.

Community
  • 1
  • 1
Vaiden
  • 15,728
  • 7
  • 61
  • 91
  • Thanks Vaiden. I know it's sort of generic question, that's why i tried to give details as much as I can (still generic, I agree). The point is, all my 15 sections are totally independent of each other, and they don't have any similarity in view (I have already reused Fragments wherever I can). The only reason I choose fragments over different activities is to use it with Navigation Drawer, as I haven't found a niche design to use NavDrawer with Activity. – abdfahim Jan 12 '17 at 01:15