Link: http://www.php100.com/html/it/biancheng/2015/0120/8419.html
Reference: http://blog.csdn.net/lmj623565791/article/details/37992017
FragmentIs an important component in Android. It is added in Android 3.0.
AboutFragmentI believe that all the developers I know should blurt out the following methods: onattach, oncreate, oncreateview,Onviewcreated, Onactivitycreated, onstart, onresume, onpause, onstop, ondestroyview, ondestroy, ondetach.
WhenFragmentIn a static way, that is, when other controls are set in the layout file, its lifecycle changes with the lifecycle of the activity. At this time, the method calling process of its life cycle is as follows:
1. When the layout page is displayed for the first time, the sequence of calling the lifecycle method is as follows:
2. When the screen of the phone is turned off or the screen of the phone is dimmed, the call sequence of its life cycle method is as follows:
3. When the mobile phone screen is unlocked again or the mobile phone screen is highlighted again, the sequence of calling the lifecycle method is as follows:
4.FragmentWhen you press the return key on the screen, the order of the life cycle method calls is as follows:
1 01-13 17:08:46.959 3102-3102/com.yeepay.fraglifecircletest I/FragA: onPause2 01-13 17:08:46.959 3102-3102/com.yeepay.fraglifecircletest I/FragA: onStop3 01-13 17:08:46.959 3102-3102/com.yeepay.fraglifecircletest I/FragA: onDestroyView4 01-13 17:08:46.959 3102-3102/com.yeepay.fraglifecircletest I/FragA: onDestroy5 01-13 17:08:46.959 3102-3102/com.yeepay.fraglifecircletest I/FragA: onDetach
However, when fragmentmanager is used for dynamic managementFragmentWhen it comes to addtobackstack, the presentation of its lifecycle is slightly complicated. But it is still not complicated to be incomprehensible.
Okay. Let's look at these questions.
First, we have rewritten twoFragmentThey overwrite their lifecycle methods and display the call of their methods by printing logs in their lifecycle methods.
The two classes are:
package com.yeepay.fraglifecircletest.frag;import android.app.Activity;import android.app.Fragment;import android.os.Bundle;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import com.yeepay.fraglifecircletest.R;public class FragA extends Fragment { private static final String TAG = FragA.class.getSimpleName(); @Override public void onAttach(Activity activity) { super.onAttach(activity); Log.i(TAG, "onAttach"); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.i(TAG, "onCreate"); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.i(TAG, "onCreateView"); return inflater.inflate(R.layout.fragment_test_a, null, false); } @Override public void onViewCreated(View view, Bundle savedInstanceState) { Log.i(TAG, "onViewCreated"); super.onViewCreated(view, savedInstanceState); } @Override public void onDestroy() { Log.i(TAG, "onDestroy"); super.onDestroy(); } @Override public void onDetach() { Log.i(TAG, "onDetach"); super.onDetach(); } @Override public void onDestroyView() { Log.i(TAG, "onDestroyView"); super.onDestroyView(); } @Override public void onStart() { Log.i(TAG, "onStart"); super.onStart(); } @Override public void onStop() { Log.i(TAG, "onStop"); super.onStop(); } @Override public void onResume() { Log.i(TAG, "onResume"); super.onResume(); } @Override public void onPause() { Log.i(TAG, "onPause"); super.onPause(); } @Override public void onActivityCreated(Bundle savedInstanceState) { Log.i(TAG, "onActivityCreated"); super.onActivityCreated(savedInstanceState); }}FragA.java
package com.yeepay.fraglifecircletest.frag;import android.app.Activity;import android.app.Fragment;import android.os.Bundle;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import com.yeepay.fraglifecircletest.R;public class FragB extends Fragment { private static final String TAG = FragB.class.getSimpleName(); @Override public void onAttach(Activity activity) { super.onAttach(activity); Log.i(TAG, "onAttach"); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.i(TAG, "onCreate"); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.i(TAG, "onCreateView"); return inflater.inflate(R.layout.fragment_test_b, null, false); } @Override public void onViewCreated(View view, Bundle savedInstanceState) { Log.i(TAG, "onViewCreated"); super.onViewCreated(view, savedInstanceState); } @Override public void onDestroy() { Log.i(TAG, "onDestroy"); super.onDestroy(); } @Override public void onDetach() { Log.i(TAG, "onDetach"); super.onDetach(); } @Override public void onDestroyView() { Log.i(TAG, "onDestroyView"); super.onDestroyView(); } @Override public void onStart() { Log.i(TAG, "onStart"); super.onStart(); } @Override public void onStop() { Log.i(TAG, "onStop"); super.onStop(); } @Override public void onResume() { Log.i(TAG, "onResume"); super.onResume(); } @Override public void onPause() { Log.i(TAG, "onPause"); super.onPause(); } @Override public void onActivityCreated(Bundle savedInstanceState) { Log.i(TAG, "onActivityCreated"); super.onActivityCreated(savedInstanceState); }}FragB.java
1. When we add Fraga in the following ways,
1 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();2 fragA = new FragA();3 fragmentTransaction.replace(R.id.frag_container, fragA, fragNames[0]);4 fragmentTransaction.commit();
Its lifecycle is displayed in the same way as the static settings in the layout file. You can check the above content.
2. When Fraga is displayed in the following way without addtobackstack,
@Override public void onClick(View v) { FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); switch (v.getId()) { case R.id.button1: if (fragA == null) { fragA = new FragA(); fragmentTransaction.replace(R.id.frag_container, fragA, fragNames[0]);// fragmentTransaction.addToBackStack(fragNames[0]); } else { Fragment fragment = fragmentManager.findFragmentByTag(fragNames[0]); fragmentTransaction.replace(R.id.frag_container, fragment, fragNames[0]); } break; case R.id.button2: if (fragB == null) { fragB = new FragB(); fragmentTransaction.replace(R.id.frag_container, fragB, fragNames[1]);// fragmentTransaction.addToBackStack(fragNames[1]); } else { Fragment fragment = fragmentManager.findFragmentByTag(fragNames[1]); fragmentTransaction.replace(R.id.frag_container, fragment, fragNames[1]); } break; default: break; } fragmentTransaction.commit(); }
The call sequence of Fraga lifecycle is:
At this point, if you click another button B, fragb will be displayed. the lifecycle of Fraga and fragb will be displayed as follows:
It can be seen that the call sequence of Fraga is onpause, onstop, ondestroyview, ondestroy, and ondetach. This indicates that Fraga has been completely abandoned by fragmentmanager and replaced by the full display of fragb. If you press the return key, the lifecycle of fragb will also be onpause, onstop, ondestroyview, ondestroy, and ondetach. This indicates thatFragmentIf the addtobackstack method is not called, The fragmentmanager is changed.FragmentIs not savedFragment.
3. We will replaceFragmentWhen addtobackstack is ready, its life cycle is displayed as follows:
replace FragA and addToBackStack########################################################################################01-13 17:08:43.359 3102-3102/com.yeepay.fraglifecircletest I/FragA: onAttach01-13 17:08:43.359 3102-3102/com.yeepay.fraglifecircletest I/FragA: onCreate01-13 17:08:43.359 3102-3102/com.yeepay.fraglifecircletest I/FragA: onCreateView01-13 17:08:43.359 3102-3102/com.yeepay.fraglifecircletest I/FragA: onViewCreated01-13 17:08:43.359 3102-3102/com.yeepay.fraglifecircletest I/FragA: onActivityCreated01-13 17:08:43.359 3102-3102/com.yeepay.fraglifecircletest I/FragA: onStart01-13 17:08:43.359 3102-3102/com.yeepay.fraglifecircletest I/FragA: onResume
It can be seen that there is no difference between calling the lifecycle method at this time and without addtobackstack.
Click "B" and use "fragb" to replace "Fraga". In this case, the call sequence of the Fraga and fragb Life Cycle methods is as follows:
and then replace FragB and addToBackStack&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&01-13 17:08:46.959 3102-3102/com.yeepay.fraglifecircletest I/FragA: onPause01-13 17:08:46.959 3102-3102/com.yeepay.fraglifecircletest I/FragA: onStop01-13 17:08:46.959 3102-3102/com.yeepay.fraglifecircletest I/FragA: onDestroyView01-13 17:08:46.959 3102-3102/com.yeepay.fraglifecircletest I/FragB: onAttach01-13 17:08:46.959 3102-3102/com.yeepay.fraglifecircletest I/FragB: onCreate01-13 17:08:46.959 3102-3102/com.yeepay.fraglifecircletest I/FragB: onCreateView01-13 17:08:46.959 3102-3102/com.yeepay.fraglifecircletest I/FragB: onViewCreated01-13 17:08:46.959 3102-3102/com.yeepay.fraglifecircletest I/FragB: onActivityCreated01-13 17:08:46.959 3102-3102/com.yeepay.fraglifecircletest I/FragB: onStart01-13 17:08:46.959 3102-3102/com.yeepay.fraglifecircletest I/FragB: onResume
It can be seen that the Fraga life cycle method only calls ondestroyview, while ondestroy and ondetach are not called. This indicates that the Fraga interface has been destroyed, but fragmentmanager has not completely destroyed Fraga, fraga is still saved in fragmentmanager.
Click a and use Fraga to replace the currently displayed fragb. In this case, the call sequence of the Fraga and fragb Life Cycle methods is as follows:
and then replace FragA again&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&01-13 17:08:51.869 3102-3102/com.yeepay.fraglifecircletest I/FragB: onPause01-13 17:08:51.869 3102-3102/com.yeepay.fraglifecircletest I/FragB: onStop01-13 17:08:51.869 3102-3102/com.yeepay.fraglifecircletest I/FragB: onDestroyView01-13 17:08:51.869 3102-3102/com.yeepay.fraglifecircletest I/FragA: onCreateView01-13 17:08:51.869 3102-3102/com.yeepay.fraglifecircletest I/FragA: onViewCreated01-13 17:08:51.869 3102-3102/com.yeepay.fraglifecircletest I/FragA: onActivityCreated01-13 17:08:51.869 3102-3102/com.yeepay.fraglifecircletest I/FragA: onStart01-13 17:08:51.869 3102-3102/com.yeepay.fraglifecircletest I/FragA: onResume
It can be seen that the call sequence of fragb's life methods is the same as that of Fraga when fragb replaces Fraga. The function is to destroy only the view, but keep it.Fragment. At this time, Fraga calls are worth noting. At this time, Fraga is directly called from oncreateview, that is, it only creates a new view, but still usesFragmentStatus.
OK. At this time, is it true?FragmentAre you familiar with addtobackstack?
Okay, the last question. Is aboutFragmentCall the lifecycle method of show and hide during fragmentmanager management.
The call implementation method is as follows:
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); switch (v.getId()) { case R.id.button1: hideAllFrags(fragmentTransaction); if (fragA == null) { fragA = new FragA(); fragmentTransaction.add(R.id.frag_container, fragA, fragNames[0]); fragmentTransaction.addToBackStack(fragNames[0]); } else { fragmentTransaction.show(fragA); } break; case R.id.button2: hideAllFrags(fragmentTransaction); if (fragB == null) { fragB = new FragB(); fragmentTransaction.add(R.id.frag_container, fragB, fragNames[1]); fragmentTransaction.addToBackStack(fragNames[1]); } else { fragmentTransaction.show(fragB); } break; default: break; }fragmentTransaction.commit();
If you are careful, you can find thatFragmentInstead of using replace. And directly addtobackstack. In fact, this can also be understood. You think, fragmentmanager must already exist in show or hide, or you need to add it if it does not exist.Fragment. This is what you need to pay attention to when using show and hide, that is, using the ADD and addtobackstack methods.
When you click a, Fraga calls the following order:
01-15 16:57:20.390 9225-9225/com.yeepay.fraglifecircletest I/hideAllFrags: hideAllFrags01-15 16:57:20.390 9225-9225/com.yeepay.fraglifecircletest I/FragA: onAttach01-15 16:57:20.390 9225-9225/com.yeepay.fraglifecircletest I/FragA: onCreate01-15 16:57:20.390 9225-9225/com.yeepay.fraglifecircletest I/FragA: onCreateView01-15 16:57:20.390 9225-9225/com.yeepay.fraglifecircletest I/FragA: onViewCreated01-15 16:57:20.390 9225-9225/com.yeepay.fraglifecircletest I/FragA: onActivityCreated01-15 16:57:20.390 9225-9225/com.yeepay.fraglifecircletest I/FragA: onStart01-15 16:57:20.390 9225-9225/com.yeepay.fraglifecircletest I/FragA: onResume
It can be seen that there is nothing different from the above.
Then, when you click "B", Fraga and fragb are called in the following sequence:
01-15 16:57:23.360 9225-9225/com.yeepay.fraglifecircletest I/hideAllFrags: hideAllFrags01-15 16:57:23.360 9225-9225/com.yeepay.fraglifecircletest I/FragB: onAttach01-15 16:57:23.360 9225-9225/com.yeepay.fraglifecircletest I/FragB: onCreate01-15 16:57:23.360 9225-9225/com.yeepay.fraglifecircletest I/FragB: onCreateView01-15 16:57:23.370 9225-9225/com.yeepay.fraglifecircletest I/FragB: onViewCreated01-15 16:57:23.370 9225-9225/com.yeepay.fraglifecircletest I/FragB: onActivityCreated01-15 16:57:23.370 9225-9225/com.yeepay.fraglifecircletest I/FragB: onStart01-15 16:57:23.370 9225-9225/com.yeepay.fraglifecircletest I/FragB: onResume
It can be seen that Fraga does not call the lifecycle method. This shows that the lifecycle of Fraga does not change when fragb is displayed. The lifecycle of fragb is the same as that of Fraga when you click a for the first time.
Then click A and B. The output log is as follows:
1 01-15 16:57:25.220 9225-9225/com.yeepay.fraglifecircletest I/hideAllFrags: hideAllFrags2 01-15 16:57:44.990 9225-9225/com.yeepay.fraglifecircletest I/hideAllFrags: hideAllFrags3 01-15 16:57:47.350 9225-9225/com.yeepay.fraglifecircletest I/hideAllFrags: hideAllFrags4 01-15 16:57:48.020 9225-9225/com.yeepay.fraglifecircletest I/hideAllFrags: hideAllFrags
This means that After Fraga and fragb are added to the backstack, their lifecycles no matter how they are shown or hide.
When the screen is locked or dimmed, and then unlocked or brightened, the call sequence of Fraga and fragb's lifecycle methods is as follows:
when screen is locked:###########################################################################################01-15 16:58:36.840 9225-9225/com.yeepay.fraglifecircletest I/FragA: onPause01-15 16:58:36.840 9225-9225/com.yeepay.fraglifecircletest I/FragB: onPause01-15 16:58:36.870 9225-9225/com.yeepay.fraglifecircletest I/FragA: onStop01-15 16:58:36.880 9225-9225/com.yeepay.fraglifecircletest I/FragB: onStopwhen screen is unlocked:##########################################################################################01-15 17:05:01.850 9225-9225/com.yeepay.fraglifecircletest I/FragA: onStart01-15 17:05:01.850 9225-9225/com.yeepay.fraglifecircletest I/FragB: onStart01-15 17:05:01.870 9225-9225/com.yeepay.fraglifecircletest I/FragA: onResume01-15 17:05:01.870 9225-9225/com.yeepay.fraglifecircletest I/FragB: onResume
We can see that twoFragmentOnpause, onstop, onstart, and onresume are all called. In addition, Fraga is called before fragb, which indicates that it is related to the order in which they are added to the backstack.
Fragment lifecycle Summary