Basic concepts of fragments

Fragement is an API introduced by Android3.0, touted as a way to address screen fragmentation and help reuse code construction. Fragments, fragments, or sub-acts are lighter and more flexible than activities.

Fragments depend on activities and cannot exist independently. An activity can have multiple fragments, and the same fragment can be reused by multiple activities.

Fragment life cycle

  • OnAttach: Fragment is attached to an activity that has already been passed in. You can use getActivity to communicate with the host activity only once
  • OnCreate: The system calls this method when creating a fragment and instantiates variables in the fragment. The variables defined in this fragment hold data during pause and stop and are called only once. If you want to start a background thread for your fragment, you can do so in this method.
  • OnCreateView: The first time you use the Fragment, the fragment layout will be loaded here. To draw the control, return a view. Try not to write too many time-consuming operations in this method.
  • OnActivityCreated: This method is called after the activity’s onCreate completes, so instead of using the onCreateView method to interact with the activity UI, you can use this method to interact with the activity
  • OnStart: Callback to the fragment when the fragment is started
  • OnResume: Fragment gains focus
  • OnPause: The user leaves the fragment without selling it
  • OnStop: Activity stopped OR fragment removed but added to rollback stack, a stopped fragment is still alive and will be removed if it is not used for a long time
  • OnDestroyView: Callback when the fragment layout is removed,
  • OnDestroy: Destroys the Fragment object
  • OnDetach: Unassociate the activity

Note: The difference between using and not using a rollback stack:

If no rollback is used, oldFragment will execute onDestroyView ->onDestroy->onDetach; If you use a rollback stack, the oldFragment will only go to onDestroyView and start loading a new fragment. The oldFragment is not killed

The creation of a Fragment

There are two ways to create a fragment, either statically or dynamically.

1. Add fragments statically

2. Dynamically add fragments

Note: Statically added fragments cannot be deleted at runtime. You are advised to use dynamic fragments because they are flexible.

Fragments of the interaction

Precautions when using Fragment

1. There are many methods to fragment that can be overridden. The most common is onCreateView

 @Override

     public View onCreateView(LayoutInflater inflater, ViewGroup container,

                              Bundle savedInstanceState) {

         // Inflate the layout for this fragment

         View root=inflater.inflate(R.layout.fragment_edit_info_home,container,false);

         return root;

     }
Copy the code

Note that the third argument is false, because in the internal fragment implementation the layout is added to the Container; if true, it is added twice and an exception is thrown.

2. If you pass in parameters when creating a Fragment, you can add them in setArguments, because with this method, even if the Fragment is killed because of memory constraints, the data can be retained when the Fragment is restored.

3. Do not define the Fragment control in onCreateView. Define it in onActivityCreated because onCreateView is called before the Activity’s onCreate is complete

4. If you want to use nested Fragments, get getChildFragmentManager in the Fragment onStart method. Why not get getChildFragmentManager in onctivityCreated? Because the fragment is not running yet, the activity is running. In the onStart method, the fragment is running and can manage its controls