Should I replace Android actions with fragments?

I have a great Android game in which there is activity for each logical screen. (Screensaver, initial screen, level selection, game screen and Settings - different actions). Now everything is working fine.

If I rewrite everything so that there is only one action, and the logical screens are fragments, will it reduce the consumption of RAM or CPU?

+5
source share
4 answers

After several years (two +) of the words “Fragments are the way”, I will never again replace actions with fragments.

Using fragments to reuse some components is fine. Using fragments for dialogs is also great, but now I realized how terrible the implementation of Fragment is, how terrible the life cycle of a fragment is, and how unpredictable (and buggy) the FragmentManager tends to be in certain circumstances. Go ahead, spend some time searching, and you will find all the “extreme, but not so extreme” cases where hacks must be implemented to get around the “design” buggy.

Sometimes you need to extend or copy the source code of these classes from the Android source code to change a private or protected field ...

Don't get me wrong, Fragments work. But they are not the solution to all your problems (perhaps they are a source of new ones in the medium term). If you already have classes, enjoy it! In fact, the new Transition Framework with Shared Elements is a clear indication that Google wants you to use more actions;)

This is my personal opinion after working in about six projects with a large Android size (some of them are popular, and you probably used them!);)

+3
source

As far as I know, no , Fragments (nearby) do not affect RAM or CPU.

An activity contains certain elements and performs some functions. A fragment is loaded onto some basic activity, such as an Activity with nothing more than an ActionBar. The rest is filled with a fragment.

Also check:

android - need some explanation of fragments to actions and views

The activity or fragment that is best suited to increase productivity and reliability?

+2
source

No, this will probably increase it (since you will have more classes), but only slightly.

The advantage of using fragments is to have reusable “blocks” that you can move around depending on your needs. For example, for a specific action, you can have a layout where you have the main window on the screen, and clicking on an element creates a new action with some details. Using fragments, you can create a layout for tablets, where the main window occupies only half the screen, and the rest is used for a fragment of the part, without having to rewrite everything.

0
source

The main advantage of fragments for me is the simple exchange of data. Between two actions, data should be transmitted in relatively primitive types ... string, int, arrayList, etc. However, between fragment and activity, data can be transferred back and forth in complex classes.

0
source

Source: https://habr.com/ru/post/1216614/


All Articles