8

I have a large Android game in which there is an Activity for each logical screen. (Splash screen, start screen, level chooser, game screen and Settings are distinct Activities). Everything is working fine right now.

If I rewrite everything so that there is only one activity and the logical screens are Fragments, Will it reduce RAM or CPU consumption?

Hello_World
  • 387
  • 3
  • 13
  • 1
    This may help you:http://stackoverflow.com/questions/20306091/dilemma-when-to-use-fragments-vs-activities – keshav kowshik Apr 01 '15 at 14:16
  • it may not reduce RAM or CPU consumption much but it will surely reduce your peace of mind. So recreate with fragments only if you want to create reusable code and utilize modular design. – Mehul Joisar Apr 01 '15 at 14:20

4 Answers4

4

After years (two+) of saying "Fragments are the way to go", I would never replace activities with Fragments again.

Using fragments to rehuse certain components is fine. Using fragments for dialogs is also fine, but I have now realized how awful the Fragment implementation is, how awful the Fragment lifecycle is and how unpredictable (and buggy) FragmentManager tends to be under certain circumstances. Go ahead an spend some time googling around and you will find all the "edge but not so edge" cases where hacks have to be implemented to work around a "by design" buggy behavior.

Sometimes you have to extend or copy the source code of these classes from the Android Source Code to modify a private or protected field…

Don't get me wrong, Fragments work. But they are not the solution to all your problems (they are possibly the source of new ones in the mid-long term). If you already have Activities, enjoy that! In fact, the new Transition Frameworks with Shared Elements is a clear indication that Google wants you to use more activities ;)

This is my personal opinion after working in roughly six mid-large sized Android projects (some are popular and you've probably used them!) ;)

Martin Marconcini
  • 26,875
  • 19
  • 106
  • 144
2

As far as I know, no, Fragments have (near to) no impact on RAM or CPU.

An Activity contains certain elements and performs some functionality. A Fragment is loaded onto an sort of base Activity, like an Activity with no more than an ActionBar. The rest is filled by the Fragment.

Also check out:

android - need some clarifications of fragments vs activities and views

Activity or Fragment which is better way to use for performance and reliable?

Community
  • 1
  • 1
Edwin Lambregts
  • 408
  • 6
  • 22
0

No, it will probably increase it (as you will have more classes) but only marginally.

The benefit of using fragments is to have reusable "blocks" that you can move around depending on your needs. For example for a specific activity you could have a layout where you have your main window on screen and clicking on an item creates an new activity with some details. With fragments, you could create a layout for tablets where the main window takes only half the screen and the rest is used for the details fragment, witout having to rewrite everything.

Evripidis Drakos
  • 872
  • 1
  • 7
  • 15
0

The main benefit of fragments to me is easy data sharing. Between two activities, data has to be passed in relatively primitive types...string, int, arrayList, etc. However between fragment and activity, data can passed back and forth in complex classes.

scubasteve623
  • 637
  • 4
  • 7