Static Fragments and Dynamic Fragments

What are the advantages and disadvantages of using a static fragment defined in XML rather than dynamic in Java? Here are the categories: 1. Maintenance, 2. Compatibility, and 3. Performance, 4. UX

+8
android android-layout android-fragments
source share
1 answer

There is a big difference in using fragments via XML or dynamically using java code, depending on what you choose, the main difference will be how the user experiences the application flow.

TL; DR : the following characteristics are shared both by fragments installed via XML and dynamically using java code (via the FragmentManager API), but Java code allows you to modify, add, delete each fragment at runtime, which allows flexibility , which it would be impossible otherwise.

enter image description here

But to answer your question:

1 - Maintaining health is pretty much the same, you use the android: name attribute to define the fragment class that you want to use VS using:

 // get fragment manager FragmentManager fm = getFragmentManager(); // add fragment FragmentTransaction ft = fm.beginTransaction(); ft.add(R.id.your_placehodler, new YourFragment()); // alternatively add it with a tag // trx.add(R.id.your_placehodler, new YourFragment(), "detail"); ft.commit(); 

In the case of only one transaction, in any case there is no effort.

2 - Compatibility: about the same , both are often used by developers everywhere

3 - Productivity, a one-time inflation process can be a little faster, but they are even significant .

4 - UX: here, depending on your needs, there can be no difference or huge .

So what is the big difference between XML or “dynamic snippets”?

Fragments created using direct XML inflation cannot be dynamically managed at run time through the FragmentManager.

As an example, this means that after you inflate a fragment / layout, you cannot send it to a freeze frame and quickly bring another advance. Dynamic fragment management can be used to ensure that this fast single-threaded experience within one multi-purpose activity is not available to the user if you move from one action to another.

After the initial FragmentTransaction above, you have absolute freedom to very quickly use the same layout for other fragments

 // replace FragmentTransaction ft = fm.beginTransaction(); ft.replace(R.id.your_placehodler, new YourFragment()); ft.commit(); // remove Fragment fragment = fm.findFragmentById(R.id.your_placehodler); FragmentTransaction ft = fm.beginTransaction(); ft.remove(fragment); ft.commit(); 

“So why do we have XML fragments? We need to ban them right now!”

Not so fast if, for example, your goal is simply to have a multi-zone window / layout in your application that will not change too much during its lifetime (that is, if you can stick with this layout until activity is rejected) xml inflation would have no flaws, and depending on the number of fragments, you could save a lot of code for the FragmentTransactions template.

Another important use case can simply be prepared for the use of another Configuration device, such as screen size or other screen orientation (for example, when using the application on both tablets and portrait / landscape phones ) XML is a simple solution to provide a different layout for each of these (and other) device configurations. same application, different device configuration

Sources: link

link

link

link

+3
source share

All Articles