Last Updated:

What is Android Fragment: Lifecycle and Snippets Use Case

We continue to develop for Android and in this article we will get acquainted with Android Fragment. Our program will consist of two pieces and one activity.


What is Fragment in Android?

The class is used to build a dynamic interface for android applications and interact with Activity. The main advantage of fragments is the simplification of working with the UI (user interface) on screens of different sizes. A single Activity can interact with an unlimited number of fragments.Fragment

Android Fragment is not a subclass like most other UI components. Instead, the slice contains a view object within itself. This view is displayed in the Activity to which the snippet is attached. Since Android Fragment is not a view, adding it to an Activity is not like some . The snippet is added to the ViewGroup inside the Activity, and the view fragment is displayed inside that ViewGroup.ViewTextView

How does Fragment work with Activity?

Let's look at the sequence of interaction of the activity with the fragment:

  1. The Activity gets a reference to the snippet.
  2. It then gets a reference to the ViewGroup in which the view of the fragment will be rendered.
  3. After that, the activity adds a fragment.
  4. The snippet creates its view and returns its Activity.
  5. This view is inserted into the ViewGroup and the snippet starts working.

How a snippet relates to other classes

The class was added back in Android along with other closely related classes:FragmentAPI 11

  1. Class — the base class for working with
  2. Class is a class for working with fragments inside the
  3. The class provides a set of operations for working with

Also at the disposal of the programmer is a package of compatibility libraries provided by Google to support versions smaller than . For example, a class is a subclass of Activity that provides work with fragments.Android API

Fragment life cycle

The fragment lifecycle is managed by 10 methods:

  • The method will be called very first even before the method, thereby attaching the fragment to the activity.onAttach()onCreate()
  • Method. The system calls this method when the fragment is drawn for the first time.onCreateView()
  • The method will be called immediately after .onViewCreated()onCreateView()
  • The method will be called after the methods and . It can be used to initialize fragment objects.onActivityCreated()onCreate()onCreateView()
  • The method is called once, and the fragment becomes visible to the user.onStart()
  • The method is called by the system when the user leaves the fragment. This method typically saves the results of the user's work.onPause()
  • Method. The fragment will be stopped by calling the .onStop()onStop()
  • The method is called before the . The method is the opposite of the method in which we created the user interface.onDestroyView()onDestroy()onCreateView()
  • The method is called to clear the state of the fragment, but the Android system does not guarantee that it will be called.onDestroy()
  • The method will be called after to notify us that the fragment has been finally separated from the Activity.onDetach()onDestroy()

Example of working with Android Fragment

Now we'll write an application with one MainActivity and two fragments: MainFragment and DetailsFragment. To do this, we need a basic Hello World project (who does not know how to create a new project in Android Studio, I advise you to start with it first).

And let's start with the layout for MainActivity:activity_main.xml

Note that we use an attribute to explicitly assign a fragment layout to a specific class.class

With the help of an attribute, we determine the weight of each fragment, or rather how much space on the screen this or that fragment will receive.layout_weight

The code for the MainActivity class is as follows:

In the MainActivity code itself, we have not changed below. The installation of fragments took place in the layout, so when the application starts, we will immediately see the fragments.activity_main

An alternative to this method is to explicitly work with the MainActivity class. We will get acquainted with this method in the following articles. Now we just have to understand how the fragments work.FragmentManager

The layout is shown below:main_fragment.xml

Class Listing :

The layout is shown below:details_fragment.xml

The class in the listing

Now let's run the application and see the results of the work:


Android Fragment

As you can see by clicking on the items in the snippet, we change the text to in the snippet. Stay tuned to the Android App Development section.