0

I have 5 complex Fragments for each Navigation menu click. However, I am not sure if I should use activities instead of fragments, because all my fragments are quite complex and expensive.

Since they are all fragments, they are also running on ONE Activity.

My question now is, would I benefit from using activities instead of fragments ?

I do know the differences of Fragments, activities. I'm just not sure if I should store all the complex fragments in one activity, or just use multiple activities instead of fragments in a navigationdrawer. I'm scared that all the fragments will consume too much ressources

Thanks a lot.

thelearner
  • 1,440
  • 3
  • 27
  • 58
  • Possible duplicate of [Android - I need some clarifications of fragments vs activities and views](https://stackoverflow.com/questions/10478233/android-i-need-some-clarifications-of-fragments-vs-activities-and-views) – Mohamed Krayem Nov 12 '17 at 11:03
  • I do know the differences of Fragments, activities. I'm just not sure if I should store all the complex fragments in one activity, or just use multiple activities instead of fragments in a navigationdrawer. I'm scared that my fragments will consume too many ressources – thelearner Nov 12 '17 at 11:24
  • 1
    It won't matter if the code for all fragments is in the same activity or not. What does matter is that you don't initialize all fragments at once (Fragment f = new SomeFragment();) but only do it when you are actually going to use them (i.e. on navigation drawer click). – Dmitri Borohhov Nov 12 '17 at 11:37
  • thanks for your help :) – thelearner Nov 13 '17 at 16:15

1 Answers1

1

This is not an expert response, but just visually I never saw Fragments perform better or worse that Activities. I also had a Navigation Drawer with activities and fragments. At the same time, writing Fragments is easier than Activities if you need to pass information along, that's a big win for Fragments. If your application lags, better consider the following points:

  • Do not run any expensive operations on the UI thread. Examples are database queries, internet calls, populating long Listviews or RecyclerViews. Use an AsyncTask for that purpose.

  • Avoid very complicated nested layouts. If possible, use a ConstraintLayout which allows you to place items arbitrarily without nested layouts.

  • Use appropriate animations if the user has to wait. It is much more rewarding than staring at a frozen screen.

  • Check your code against the Google's Best Practices for Performance.

If you have a more specific point where your application is getting slow, please post your code and describe the problem in more detail.

Dmitri Borohhov
  • 1,513
  • 2
  • 17
  • 33