Fragments, while not officially one of the four components, are used almost as often in a project as the other three non-activity components.

Because it is often needed to use, so it is often asked about the knowledge point. The interview about Fragment mainly focuses on two points: 1. The life cycle of interaction with the Activity and the change of life cycle using add & Replace; 2. This paper will elaborate on these two points.

I. Fragment Life cycle

1.Fragment Complete life cycle

The 2021-08-07 10:50:13. 540, 5066-5066 /? D/MyFragment-TEST: onAttach 2021-08-07 10:50:13.541 5066-5066/? D/MyFragment -test: onCreate 2021-08-07 10:50:13.548 5066-5066/? D/MyFragment-TEST: onViewCreated 2021-08-07 10:50:13.548 5066-5066/? D/MyFragment-TEST: onActivityCreated 2021-08-07 10:50:13.548 5066-5066/? D/MyFragment-TEST: onViewStateRestored 2021-08-07 10:50:13.549 5066-5066/? D/MyFragment -test: onStart 2021-08-07 10:50:13.554 5066-5066/? D/MyFragment-TEST: OnResume -- -- -- -- -- -- lock screen -- -- -- -- -- -- - the 2021-08-07 10:51:11. 606, 5066-5066 / com. Example. Myfirstproject D/MyFragment - TEST: OnPause 10:51:11. 2021-08-07, 668, 5066-5066 / com. Example. Myfirstproject D/MyFragment - TEST: OnStop 10:51:11. 2021-08-07, 675, 5066-5066 / com. Example. Myfirstproject D/MyFragment - TEST: OnSaveInstanceState -- -- -- -- -- -- XieBing -- -- -- -- -- the 2021-08-07 10:51:29. 387, 5066-5066 / com. Example. Myfirstproject D/MyFragment - TEST: OnStart 10:51:29. 2021-08-07, 389, 5066-5066 / com. Example. Myfirstproject D/MyFragment - TEST: OnResume -- -- -- -- -- - the -- -- -- -- -- - the 2021-08-07 10:51:38. 153, 5066-5066 / com. Example. Myfirstproject D/MyFragment - TEST: OnPause 10:51:38. 2021-08-07, 154, 5066-5066 / com. Example. Myfirstproject D/MyFragment - TEST: OnStop 10:51:38. 2021-08-07, 155, 5066-5066 / com. Example. Myfirstproject D/MyFragment - TEST: OnDestroyView 10:51:38. 2021-08-07, 172, 5066-5066 / com. Example. Myfirstproject D/MyFragment - TEST: OnDestroy 10:51:38. 2021-08-07, 173, 5066-5066 / com. Example. Myfirstproject D/MyFragment - TEST: onDetachCopy the code

OnAttach () : Called when a Fragment is associated with an Activity. You can use this method to get the Activity reference, and you can also get the arguments with getArguments().

OnCreate () : called when the Fragment is being created

OnActivityCreated () : Called when the Activity completes onCreate()

OnStart () : called when the Fragment is visible.

OnResume () : called when the Fragment is visible and interactive

OnPause () : called when the Fragment is not interoperable but visible.

OnStop () : called when the Fragment is not visible.

OnDestroyView () : Called when the Fragment’s UI is removed from the view structure.

OnDestroy () : called when the Fragment is destroyed.

OnDetach () : called when a Fragment is disassociating from an Activity.

2. The lifecycle of the Fragment and Activity interaction

The 2021-08-07 20:32:48. 882, 13191-13191 / com. Example. Myfirstproject D/MainActivity - TEST: OnCreate 20:32:49. 2021-08-07, 051, 13191-13191 / com. Example. Myfirstproject D/MyFragment - TEST: OnAttach 20:32:49. 2021-08-07, 051, 13191-13191 / com. Example. Myfirstproject D/MainActivity - TEST: OnAttachFragment 20:32:49. 2021-08-07, 051, 13191-13191 / com. Example. Myfirstproject D/MyFragment - TEST: OnCreate 20:32:49. 2021-08-07, 057, 13191-13191 / com. Example. Myfirstproject D/MyFragment - TEST: OnViewCreated 20:32:49. 2021-08-07, 057, 13191-13191 / com. Example. Myfirstproject D/MyFragment - TEST: OnActivityCreated 20:32:49. 2021-08-07, 057, 13191-13191 / com. Example. Myfirstproject D/MyFragment - TEST: OnViewStateRestored 20:32:49. 2021-08-07, 058, 13191-13191 / com. Example. Myfirstproject D/MyFragment - TEST: OnStart 20:32:49. 2021-08-07, 058, 13191-13191 / com. Example. Myfirstproject D/MainActivity - TEST: OnStart 20:32:49. 2021-08-07, 062, 13191-13191 / com. Example. Myfirstproject D/MainActivity - TEST: OnResume 20:32:49. 2021-08-07, 062, 13191-13191 / com. Example. Myfirstproject D/MyFragment - TEST: OnResume -- -- -- -- -- -- -- -- -- the -- -- -- -- -- - the 2021-08-07 20:35:03. 357, 13411-13411 / com. Example. Myfirstproject D/MyFragment - TEST: OnPause 20:35:03. 2021-08-07, 358, 13411-13411 / com. Example. Myfirstproject D/MyFragment - TEST: OnStop 20:35:03. 2021-08-07, 361, 13411-13411 / com. Example. Myfirstproject D/MyFragment - TEST: OnDestroyView 20:35:03. 2021-08-07, 375, 13411-13411 / com. Example. Myfirstproject D/MyFragment - TEST: OnDestroy 20:35:03. 2021-08-07, 375, 13411-13411 / com. Example. Myfirstproject D/MyFragment - TEST: OnDetach 20:35:03. 2021-08-07, 691, 13411-13411 / com. Example. Myfirstproject D/MainActivity - TEST: OnPause 20:35:04. 2021-08-07, 284, 13411-13411 / com. Example. Myfirstproject D/MainActivity - TEST: OnStop 20:35:04. 2021-08-07, 292, 13411-13411 / com. Example. Myfirstproject D/MainActivity - TEST: onDestroyCopy the code

The first thing we need to know is that fragments are created attached to the Activity page. Then look at the order in which the Fragment and Activity methods are executed.

Create and rebuild processes: The Activity lifecycle takes precedence over the Fragment

Pause and destroy processes: The Fragment life cycle takes precedence over the Activity

3. Use add and replace to change the life cycle

To test the difference between Add and Relace this time, two fragments are needed. The first MyFragment is added to the container. Then Add the My2Fragment using Add and Replace, respectively. Then observe the lifecycle of MyFragment and My2Fragment respectively.

Add (R.I.C.ontainer,fragment_2);

The 2021-08-07 11:07:13. 310, 7510-7510 / com. Example. Myfirstproject D/MyFragment - TEST: OnAttach 11:07:13. 2021-08-07, 311, 7510-7510 / com. Example. Myfirstproject D/MyFragment - TEST: OnCreate 11:07:13. 2021-08-07, 318, 7510-7510 / com. Example. Myfirstproject D/MyFragment - TEST: OnViewCreated 11:07:13. 2021-08-07, 318, 7510-7510 / com. Example. Myfirstproject D/MyFragment - TEST: OnActivityCreated 11:07:13. 2021-08-07, 318, 7510-7510 / com. Example. Myfirstproject D/MyFragment - TEST: OnViewStateRestored 11:07:13. 2021-08-07, 318, 7510-7510 / com. Example. Myfirstproject D/MyFragment - TEST: OnStart 11:07:13. 2021-08-07, 324, 7510-7510 / com. Example. Myfirstproject D/MyFragment - TEST: OnResume 11:07:20. 2021-08-07, 033, 7510-7510 / com. Example. Myfirstproject D/My2Fragment - TEST: OnAttach 11:07:20. 2021-08-07, 034, 7510-7510 / com. Example. Myfirstproject D/My2Fragment - TEST: OnCreate 11:07:20. 2021-08-07, 053, 7510-7510 / com. Example. Myfirstproject D/My2Fragment - TEST: OnViewCreated 11:07:20. 2021-08-07, 053, 7510-7510 / com. Example. Myfirstproject D/My2Fragment - TEST: OnActivityCreated 11:07:20. 2021-08-07, 053, 7510-7510 / com. Example. Myfirstproject D/My2Fragment - TEST: OnViewStateRestored 11:07:20. 2021-08-07, 053, 7510-7510 / com. Example. Myfirstproject D/My2Fragment - TEST: OnStart 11:07:20. 2021-08-07, 053, 7510-7510 / com. Example. Myfirstproject D/My2Fragment - TEST: onResumeCopy the code

You can see that using Add to Add Fragment2,Fragment1 and Fragment2 do not affect each other.

Relace transaction. Replace (R.I.D.C., fragment_2)

The 2021-08-07 11:09:09. 712, 7806-7806 / com. Example. Myfirstproject D/MyFragment - TEST: OnAttach 11:09:09. 2021-08-07, 713, 7806-7806 / com. Example. Myfirstproject D/MyFragment - TEST: OnCreate 11:09:09. 2021-08-07, 723, 7806-7806 / com. Example. Myfirstproject D/MyFragment - TEST: OnViewCreated 11:09:09. 2021-08-07, 723, 7806-7806 / com. Example. Myfirstproject D/MyFragment - TEST: OnActivityCreated 11:09:09. 2021-08-07, 723, 7806-7806 / com. Example. Myfirstproject D/MyFragment - TEST: OnViewStateRestored 11:09:09. 2021-08-07, 724, 7806-7806 / com. Example. Myfirstproject D/MyFragment - TEST: OnStart 11:09:09. 2021-08-07, 730, 7806-7806 / com. Example. Myfirstproject D/MyFragment - TEST: OnResume 11:09:22. 2021-08-07, 321, 7806-7806 / com. Example. Myfirstproject D/My2Fragment - TEST: OnAttach 11:09:22. 2021-08-07, 321, 7806-7806 / com. Example. Myfirstproject D/My2Fragment - TEST: OnCreate 11:09:22. 2021-08-07, 324, 7806-7806 / com. Example. Myfirstproject D/MyFragment - TEST: OnPause 11:09:22. 2021-08-07, 324, 7806-7806 / com. Example. Myfirstproject D/MyFragment - TEST: OnStop 11:09:22. 2021-08-07, 325, 7806-7806 / com. Example. Myfirstproject D/MyFragment - TEST: OnDestroyView 11:09:22. 2021-08-07, 346, 7806-7806 / com. Example. Myfirstproject D/My2Fragment - TEST: OnViewCreated 11:09:22. 2021-08-07, 346, 7806-7806 / com. Example. Myfirstproject D/My2Fragment - TEST: OnActivityCreated 11:09:22. 2021-08-07, 346, 7806-7806 / com. Example. Myfirstproject D/My2Fragment - TEST: OnViewStateRestored 11:09:22. 2021-08-07, 346, 7806-7806 / com. Example. Myfirstproject D/My2Fragment - TEST: OnStart 11:09:22. 2021-08-07, 347, 7806-7806 / com. Example. Myfirstproject D/My2Fragment - TEST: onResumeCopy the code

Notice that I’ve separated it with a blank line, but notice that using Replace adds Fragment2 and Fragment1 destroys the onDestroyView’s lifecycle.

Replace has a destruct life cycle compared to add and replace.

2. Data communication between fragments and activities

1. The Activity transfers data to the Fragment

There are two ways to do this, as shown in the following code comment:

MyFragment fragment = new MyFragment().newInstance("hello"); // Bundle Bundle = new Bundle(); Bundle.putstring ("key", "Hello "); // Measure 2 fragment.setarguments (bundle);Copy the code

2. The Fragment transfers data to the Activity

Define an interface:

public interface SetData {
    void setKey(String key);
}
Copy the code

Declare SetData in the Fragment:

private SetData listener; //------------- listener= (SetData)context; //--------------- listener.setkey (" You are ok ");Copy the code

The Activity implements (implements) the SetData interface:

public class MainActivity extends BaseActivity implements SetData{ @Override public void setKey(String key) { Log.d("MainActivity======",key); }}Copy the code

That is, the interface callback mode is adopted.

Of course, we can also use EventBus,LiveDataBus and other related frameworks to implement in-application communication.


This is a summary of the Fragment lifecycle and communication knowledge.

Attached is a diagram of the lifecycle network of Fragment and Activity interactions: