What’s the difference between drawing in Activity and in Fragment?

Asked

Viewed 5,967 times

9

After the last SDK update in Eclipse, when creating a new project, a fragment next to activity_main. I would like to know why the fragment, since before it opened directly to activity_main. Is there any difference in the performance of the project? Thank you

1 answer

18


Since no one has ventured, I’ll try to help with what I’ve learned about Fragments.

Before entering briefly into details, I would like to clarify that the loss of performance (cost x benefit) is very derisory in relation to flexibility, reuse, evolution of smartphones (processing, storage, RAM, etc...) and the facilities that the Fragment tail.

What is a Fragment?

Roughly the Fragment is an interface "Autonomous Piece". As well as the Activity, he owns a life cycle and a state storage treatment (provided by FragmentManager), which is initiated at the time when the Fragment is linked to Activity (Attached), and not necessarily visible on the screen (It needs to be added to the layout, Added).

The Fragment appeared on Android 11 (Honeycomb), which emerged to be the ONLY1 of some Tablets, and the current configuration (Activity), did not meet and made the development of interfaces that took advantage of the available screen size much more difficult (imagine a smartphone app stretched on a tablet screen? is very bad, but this exists).

Soon, they had the idea to develop the Fragment, that makes the composition of screens dynamic (in which pieces of screen vary, whether in relation to screen orientation, size, user context, taste) much easier.

The Fragment is available in the library Support v4 of the SDK, so you can use the Fragment until versions prior to 11. What does not stop you from using now.

This image of the official android site illustrates very well this concept:

Fragments em Tablet e Handset

In this image we have 2 Fragments being presented in two of thousands of possible ways. But the important thing is that the same app for tablet and smartphone, use the same code and with very few modifications in this case, this is the most important (reuse).

In the case of Handset, me particularly do not like a little of the solution given in this image. I prefer to replace the Fragment A by B and put her in BackStack, so when the user presses the Backspace, the Fragment will return. Another issue is that in this case, some things will involve communication between Activities, what I prefer to avoid. It’s a matter of taste, everyone has their own :P

How to start using?

There are two ways to add one Fragment in a Activity.

Be in the XML of your layout, they will be insticed, Attached and Added automatically when the layout is inflated:

XML of the layout in the Tablet (is in res/layout-large)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
    <fragment android:name="com.example.news.ArticleListFragment"
        android:id="@+id/list"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent" />
    <fragment android:name="com.example.news.ArticleReaderFragment"
        android:id="@+id/viewer"
        android:layout_weight="2"
        android:layout_width="0dp"
        android:layout_height="match_parent" />
</LinearLayout>

XML of the smartphone layout (stays in: res/layout)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment android:name="com.example.news.ArticleListFragment"
        android:id="@+id/list"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent" />
</LinearLayout>

In that case, the XML above is just the Activity A, the of Activity B is analogous, but with the other Fragment.

Or programmatically:

ExampleFragment fragment = new ExampleFragment();
FragmentTransaction fragmentTransaction = getSupportFragment().beginTransaction();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();

I strongly recommend reading the official documentation on Fragments, has even an example you can do to better understand, worth it.

Pros and Cons

Activity

  • Pros
    • Simplicity of construction.
    • Life cycle management facility.
    • Ease in creating navigation between activities.
  • Cons
    • Layout is very rigid, migration difficulty to layouts "Multipane".
    • Ease of making code monolithic and difficult to maintain (Callback Hell).

Fragment

  • Pros
    • Highly modularizable.
    • Reusability.
    • Ease of building dynamic layouts.
    • It is possible to use it as a way to save states of Activity, persisting even during configuration changes (change of orientation for example).
  • Cons
    • Greater management complexity (Managing the life cycle is not as easy depending on how the Fragment is used).
    • Communication between Fragments is not trivial, needs the intervention of Activity.

These are a few pros and cons of each, it may be that from my point of view the subject is not so neutral, if someone wants to add something, is welcome.

I will not extend too much otherwise it will get too big and bad to read, if someone wants to supplement the text, is very welcome!

I leave a recommendation, by own experience: Do everything using Fragments, even if it’s a Activity with only one Fragment, because when it is to evolve your application, it becomes much easier to reuse what was done, besides having the opportunity to offer a richer interface for Tablets, which is very well seen when publishing your application in the Play Store.

  • 1

    The point exactly would be advantages and disadvantages, compared to Activity... Can you name a few please? Just to complement..

  • All right, I’ll compliment you soon

  • @GH_, as amended!

  • Thank you so much man, cleared my mind and even gave me tips on how to use the Fragment! Eternally grateful hahaha

Browser other questions tagged

You are not signed in. Login or sign up in order to post.