Fragment is a UI Fragment that can be embedded in an Activity to improve utilization of large screens and interface code on different screens.

How fragments are used

Simple use of Fragment

The following records the steps to implement a left and right split screen using fragments.

  1. Create left Fragment layout: left_fragment.xml
  2. Create the right Fragment layout: right_fragment.xml
  3. Create a classLeftFragmentAnd inheritanceFragmentClass, rewriteonCreateView()The left_fragment function loads the left Fragment layout using LayoutInflater
  4. Create a classRightFragmentAnd inheritanceFragmentClass, rewriteonCreateView()Function to load the right Fragment layout right_fragment using LayoutInflater
  5. Introduce the left and right fragments into the Activity loading layout. Note in the tag by specifyingandroid:nameTo specify the specific Fragment layout

Dynamically Adding fragments

What really makes a layout more flexible with a Fragment is that it can be added dynamically to an Activity while the program is running, and I think that’s what a Fragment is really for. The steps for dynamic loading are described below.

  1. Create a fragment in a simple use project: another_right_fragment.xml
  2. Create a corresponding classAnotherRightFragmentAnd inheritanceFragment(), rewrite functiononCreateView()
  3. Replace the Fragment tag belonging to the Right_fragment in the Activity layout with FrameLayout
  4. Add fragment replacement logic to the MainActivity code

Consider: Should I not go into detail about these simple operations in the future and go straight to the main points of the section, such as the substitution logic?

Dynamically add fragment step:

  1. Create Fragment instance to add:AnotherRightFragment()
  2. Get FragmentManager,fragmentManager = supportFragmentManager
  3. Start a transaction,val transaction = fragmentManager.beginTransaction()
  4. Using transactions to do framgNet replacement,transaction.repalce(original layout id, new fragment)
  5. Commit a transaction,transaction.commit()

Dynamically add back stack

In order to achieve a similar return stack effect, make a Fragment can return BACK key, use the transaction when replace the function of the transaction. AddToBackStack ()

Fragment interaction with activities

Getting instances of each other is a common occurrence, and the way we get instances of each other also helps us distinguish between activities, fragments, views, and layouts.

  1. Get Fragment instance from Activity:

Val fragments = supportFragmentManager. FindFragmentById (R.i d.f ragmentid) as FragmentClass, then call fragments internal function

  1. Fragment obtains an Activity instance:

Val mainActivity () = activity as mainActivity ()

  1. Communication between two fragments of the same Activity:

Fragment1 gets an instance of the Activity, and then the Activity gets an instance of Fragment2

Fragment life cycle

Fragment is a UI suite embedded in an Activity that has the same life cycle as an Activity, but also adds some additional destruction methods.

Fragment State in its life cycle

  • Running state: When the Activity associated with the Fragment is running
  • Paused: When the Activity associated with the Fragment is paused
  • Stopped state: When the Activity associated with the Fragment is stopped or the Fragment is replaced, removed, and addToBackStack(), the Fragment is invisible to the user and may be reclaimed
  • Destroy state: When the Activity associated with the Fragment is destroyed, or when the Fragment is replaced or removed without addToBackStack()

Additional callback methods

In contrast to an Activity, a Fragment needs to be actively associated with an Activity and actively load a View, so the following callback methods are added

  • OnAttach (): Associates the Activity
  • OnCreateView (): Loads the layout
  • OnActivityCreated (): Verifies that the associated Activity has been created
  • OnDestroyView (): Removes the layout
  • OnDetach (): Disassociated ps from the Acttivity. Run onDestroy() then onDetach()

The complete Fragment cycle

Dynamically loaded layout

  1. Use qualifiers: For large-screen devices, for example, create activity_main.xml in the layouts-large folder. The layout content in the file will be automatically loaded by the large-screen device
  2. Use the minimum width qualifier: for example, when running a layout on a device with a screen width of 500dp or more, create a new layout-sw500 folder and create activity_main.xml

Analysis of Fragment best practices

Need to dynamic processing is the best practice when click the response after the news headlines, for mobile phone is the new ActivityNewsContentActivity into the stack, For tablets, call the NewsContentFragment instance of the Fragment class and call refresh(News).

The solution here is to add the isTwoPane flag to the NewsTitleFragment class. IsTwoPane = Activity; isTwoPane = Activity; isTwoPane = Activity; .findViewById

(R.id.contentLayout) ! = null. Because there is a RecyclerView control inside the TitleFragment to scroll the title, when setting the corresponding Adapter and adding onClickListener for title_item, the isTwoPane value will be processed accordingly.

It is worth mentioning that controlling the content of the TextView in the ContentFragment through the Button in the TitleFragment belongs to communicating with the Fragment under the Activity. The specific method is mentioned above.