Proper use afresh hear not Activity and Fragment

By Fragment that appeared from Honeycomb, all of the controller logic is freed from the spell that would be aggregated into Activity, humanity has regained peace. Fragment is also, as a member of the Support Library, You are about to leave also erase the sin that was piled up the world earlier past Gingerbread.

But is such a Fragment, good enough to use and do not know, also heard the occasional voice called. In this paper, learn about the Activity and Fragment begun to develop Android applications, to for users who suffer from segregation of the two, I will try to introduce how to make effective Fragment.

Dark era Fragment there was no

Fragment has never been era, Will was making the Android application in the manner which is humanity.

Switching of the screen using the tab

Has a plurality of tabs, suppose you create an application, such as to display different content for each tab.

To make such an application, to achieve the display of the function and the content of the tab in the following procedure.

  1. Make the Activity to be the host to handle the tab switching
  2. Only a few of the tab, make the Activity to display the content

Although the number what not many in roughly the procedure, in this way we are hiding some problems.

  1. A large object with spite of Activity, do I need put in memory only the number of the tab of the host and the tab of content
  2. Can not be realized unless some of the dedicated components and whole bunch cooperation, poor flexibility

Use maneuvering on the layout

On the mechanism of Android layout, If you leave charged with code like the following to xml, you can achieve it is to use maneuvering on the layout resource.

<include layout="@layout/hogehoge" android:id="@+id/included_layout">

This is although very useful, because once used only in the View of the layer does not work, can not use screwdriver until the controller of the logic that controls it.

Generation and life cycle of the dialog

Dialog, Activity manages. There is a life cycle in the Activity, and the life cycle is completed must be terminated also its role, for example, when the screen rotation, also has disappeared dialog that has been displayed.

Therefore, to write code that carry over the flag relating to the display of the dialog, look at that flag, new Activity was not must decide whether or not to continue the display of the dialog.

Civilization flowering due to the emergence of Fragment

Becomes a Honeycomb, Fragment has appeared gallantly with the ActionBar.

The existing set of components for implementing the tab function @Deprecated becomes, instead, now prompted the use of Fragment.

ActionBar by, unified the behavior related to navigation in the app, along with it, you use the Fragment that can be handled easily as a combination of ActionBar began to be recommended.

The Fragment

The Javadoc of Fragment of Android Developers, located in the following manner.

A Fragment is a piece of an application's user interface or behavior that can be placed in an Activity.

Has a UI, to manage the behavior of the UI, the components that can be incorporated into the Activity is Fragment. In other words, a result, the <include>only tag could not be realized, it became to be able to turn to use the controller's logic.

In addition, not limited to the ActionBar, Fragment will be handled from the View components, such as ViewPager. Unlike the previous tab, for the interface to be used for implementation it has also been simplified, now to be able to implement the same function without spending also the mounting cost of about tab.

For Dialog, dedicated DialogFragment is provided, for now DialogFragment also be managed on the basis of the management policy of Fragment, as before, or have a flag on display, the code that said that or carry-over that flag you no longer need to implement on their own.

The advent of the multi-device era

Now Android is, not only to the handset terminal, such as a smart phone, can also be used in the tablet terminal. And, as seen in iOS universal application, a mechanism to deliver a common application is developed in the handset terminal and a tablet terminal, now one of the applications can be delivered to various terminals.

Corresponding to the difference of the size of the screen

The biggest difference in the difference of the handset terminal and the tablet terminal is the size of the screen. If the tablet terminal, it will be able to use a wider location.

At this time, when the layout was made for the handset terminal will be displayed as it is, will that become strangely screen was a temple. In this, a large wide screen of the long-awaited results in wasted.

For example, a list screen of a certain content, you have a detail screen of each item.

In the handset terminal, because the screen is small, each of the screen would be better to make as another Activity. However, in the tablet terminal, a list display only, the only display details, will be wasting considerable space. Therefore, in the tablet terminal, asked to list to the left, when you select an item from the list, and then varnish to perform a detailed display on the left side. By doing so, you can effectively take advantage of its large screen.

In such a case, the list display and detail display, If you have been handled by Fragment, corresponding to the handset terminal and a tablet terminal, will be much easier.

This is because, in the friendly handset terminal, making each of Activity for the detailed display for the list, the Activity is well if you have even logic to place the Fragment, also, to the friendly tablet terminal, one of the Activity, List use and because made to avoid if you have a logic to place the Fragment for details.

... In this way the world was wrapped in Fragment ...

In addition to this, ViewPager and Fragment is very compatible with good cooperation. Also ActionBar, there is a function of tabs that can be implemented with a simple interface, the function of the tab of ViewPager and ActionBar is, we can work together in a simple interface.

In fact, Fragment can also be an implementation that does not have a UI. Advantages of doing so, there is life cycle Fragment, it since it closely linked to that of Activity, with data, acts like a holder for management in accordance with the life cycle for the Activity There in that you can.

To design a class to fit the life cycle of Activity is haunted is troublesome. It, I Fragment is us to take over.

In addition, the more the role Activity, handles more data. Instead of holding the data as a member variable, by leaving the holding of the data to Fragment, the better the Activity of outlook.

(Oh well I feel that there is a slight exaggeration to ...)