Latest web development tutorials

Android fragment (Fragment)

Debris is part of the activity, the activity is more modular design. We can be a sub-task debris activities.

The following are important points about debris -

  • Debris has its own layout, their behavior and their lifecycle callbacks.
  • When the activity is running, you can add or remove debris in the event.
  • You can combine multiple fragments in a single activity to build UI multi-bar.
  • Debris can be used in multiple activities.
  • Fragments of the life cycle and its host activities closely related. This means that the activity was suspended all activities of the debris is stopped.
  • Debris can act with no user interface components.
  • Debris is Android API version 11 is added to the Android API.

Fragment class through inheritance to create fragments. By using Debris element declaration in the active layout file fragments to insert in your activities.

Before the introduction of debris, because every given point in time can only display a single activity on the screen, so we have a limitation. Unable to split the screen and device independent control different parts. With the introduction of debris, we gain greater flexibility, and makes a point of time only on the screen is limited to a single event has been removed. Now we can have a single activity, but each activity by a plurality of pieces assembled, each fragment has its own layout, and complete event lifecycle.

Here is a typical example demonstrates how to make two fragments defined by the UI module, in combination flat plate design activities, separated for handheld devices designed activities.

image

When running on a tablet-sized device, the application can be embedded in two pieces at the event A. Screen on mobile devices, since there is not enough space activities A only contains an article fragment list when the user clicks on the article, start the second fragment contains the activities of B to read the article.


Fragments lifecycle

Android fragmentation has its own life cycle, activities and Android is very similar. The following outlines the different stages of its life cycle.

image

This is a list of methods in the class you can override the fragment:

  • onAttach (): Debris instance is associated activity instance. Debris and activities not yet fully initialized. Typically, you get a reference to the activities in this process, debris in the future be used in the initialization.
  • onCreate (): When creating debris, the system calls the method. You need to initialize the necessary components of some debris. These components when debris is suspended, stopped the need to retain in order to be restored.
  • onCreateView (): When the first fragment will draw its user interface system calls this method. To draw debris UI, you need to return to a representative fragment root layout View component from the process. If the fragment does not provide a user interface, a direct return null.
  • onActivityCreated: When the host activity is created, the method is called after onCreateView () method. Examples of events and activities and debris view hierarchy is created. In this case, the view can be accessed by findViewById () method. In this method, you can instantiate objects require Context object.
  • onStart (): This method is called when visible debris.
  • onResume (): This method is called when the debris can interact.
  • onPause (): When the first time that a user will want to leave when the method is called fragmented system. In general, here you need to submit any changes will persist beyond the user's session.
  • onStop (): Called when the debris is to be stopped.
  • onDestroyView (): After calling this method, the debris will be destroyed.
  • onDestroy (): This method is used to clear the debris of state. But in the Android platform is not guaranteed to be called.

How to use the debris?

Here are simple steps to create debris.

  • First, decide how many events require fragmentation. For example, we need to use two pieces to handle portrait and landscape screen modes of the device.
  • Next, based on the amount of debris created class inherited from Fragment classes. Fragment class contains the callback function mentioned above. Depending on your needs rewriting any method.
  • Corresponding to each clip, you need to create a layout in the XML file. These files contain the layout defined debris.
  • Finally, based on the necessity of modifying the active file to define the actual debris replacement logic.

Debris Type

It can be divided into three basic pieces as follows:

  • Debris single frame - single frame pieces are used such as mobile telephones, handheld devices. As a fragment as a video display.
  • List of debris - containing debris called special list view list debris.
  • Transition pieces - used with fragmentation issues. You can move from one fragment to another fragment.