Hello_World
Hello_World

Reputation: 387

Should I replace Android Activities by Fragments?

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?

Upvotes: 8

Views: 1689

Answers (4)

Martin Marconcini
Martin Marconcini

Reputation: 27236

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!) ;)

Upvotes: 4

scubasteve623
scubasteve623

Reputation: 647

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.

Upvotes: 0

Edwin Lambregts
Edwin Lambregts

Reputation: 408

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?

Upvotes: 2

Evripidis Drakos
Evripidis Drakos

Reputation: 870

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.

Upvotes: 0

Related Questions