在上一篇博客,Android-fragment简介-fragment的简单使用,介绍了fragment简单的使用,还介绍了fragment简介(fragment是有生命周期的)
必须知道fragment生命周期,才能正确的使用fragment:
MyTestFragmentActivity3
package liudeli.activity.fragment; import android.app.Activity; import android.os.Bundle; import android.util.Log; import liudeli.activity.R; public class MyTestFragmentActivity3 extends Activity { private final String TAG = MyFragment3.class.getSimpleName(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test_fragment3); Log.i(TAG, "Activity onCreate"); } @Override protected void onStart() { super.onStart(); Log.i(TAG, "Activity onStart"); } @Override protected void onResume() { super.onResume(); Log.i(TAG, "Activity onResume"); } /***** 当一个Activity不再被使用时,它会经历一系列反向回调:*****/ @Override protected void onPause() { super.onPause(); Log.i(TAG, "Activity onPause"); } @Override protected void onStop() { super.onStop(); Log.i(TAG, "Activity onStop"); } @Override protected void onDestroy() { super.onDestroy(); Log.i(TAG, "Activity onDestroy"); } }
R.layout.activity_test_fragment3 Activity布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:id="@+id/fragment" class="liudeli.activity.fragment.MyFragment3" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
MyFragment3 生命周期
package liudeli.activity.fragment; 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 android.widget.TextView; /** * Fragment的生命周期 */ public class MyFragment3 extends Fragment { private final String TAG = MyFragment3.class.getSimpleName(); /** * 当Fragment附加到Activity之上时调用此方法 * @param activity */ @Override public void onAttach(Activity activity) { super.onAttach(activity); Log.d(TAG, ">>>>>>>>>>>>>>> MyFragment onAttach()"); } /** * 当Fragment初始化时调用此方法 */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG, ">>>>>>>>>>>>>>> MyFragment onCreate()"); } /** * 当Fragment创建View(1.布局加载加载Layout 2.new View)并返回 * @param inflater 布局加载器 用来加载布局 * @param container * @param savedInstanceState 保存临时数据,和Activity的onCreate->savedInstanceState 一样 * @return */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); Log.d(TAG, ">>>>>>>>>>>>>>> MyFragment onCreateView()"); TextView tv = new TextView(getActivity()); // 在Fragment不能使用this /** * 注意:⚠️ 此方法必须要返回View,否则会报错 */ return tv; } /** * Activity已创建完成 * @param savedInstanceState 保存临时数据,和Activity的onCreate->savedInstanceState 一样 */ @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); Log.d(TAG, ">>>>>>>>>>>>>>> MyFragment onActivityCreated()"); } /** * 当Fragment启动 */ @Override public void onStart() { super.onStart(); Log.d(TAG, ">>>>>>>>>>>>>>> MyFragment onStart()"); } /** * 当Fragment活跃在前台显示 */ @Override public void onResume() { super.onResume(); Log.d(TAG, ">>>>>>>>>>>>>>> MyFragment onResume()"); } /***** 当一个片段不再被使用时,它会经历一系列反向回调:*****/ /** * 当Fragment不再前台 */ @Override public void onPause() { super.onPause(); Log.d(TAG, ">>>>>>>>>>>>>>> MyFragment onPause()"); } /** * 当Fragment不再前台 不完全显示 会 onStop */ @Override public void onStop() { super.onStop(); Log.d(TAG, ">>>>>>>>>>>>>>> MyFragment onStop()"); } /** * 当Fragment销毁自己的控件 */ @Override public void onDestroyView() { super.onDestroyView(); Log.d(TAG, ">>>>>>>>>>>>>>> MyFragment onDestroyView()"); } /** * 当Fragment销毁自己 */ @Override public void onDestroy() { super.onDestroy(); Log.d(TAG, ">>>>>>>>>>>>>>> MyFragment onDestroy()"); } /** * 当Fragment 离开 Activity (不附加在Activity之上了 离开了) */ @Override public void onDetach() { super.onDetach(); Log.d(TAG, ">>>>>>>>>>>>>>> MyFragment onDetach()"); } }
以下动态有两个动作
动作一:Fragment 附加到 Activity 上
动作二:Fragment 离开Activity
打开 --> Activity--> layout<fragment class --> fragment
1.Fragment附加到Activity之上 ,执行onAttach方法
2.Fragment创建啦,执行onCreate方法
3.Fragment的View创建啦,执行onCreateView方法
4.Activity的onCreate执行了
5.Fragment收到Activity创建好啦,执行onActivityCreated方法
6.Activity的onStart执行了
7.Fragment启动啦,执行onStart方法
8.Activity的onResume执行了
9.Fragment显示到前台啦,执行onResume方法
点击返回(关闭) --> Activity--> layout<fragment class --> fragment
1.Fragment暂停啦,执行onPause方法
2.Activity的onPause执行了
3.Fragment停止了,执行onStop方法
4.Activity的onStop执行了
5.Fragment销毁自己的控件,执行onDestroyView方法
6.Fragment销毁自己,执行onDestroy方法
7.Fragment离开Activity,不再附近啦,执行onDetach方法
8.Activity销毁了,执行onDestroy方法
打开 --> Activity--> layout<fragment class --> fragment
12-26 02:07:35.066 5493-5493/? D/MyFragment3: >>>>>>>>>>>>>>> MyFragment onAttach()
12-26 02:07:35.066 5493-5493/? D/MyFragment3: >>>>>>>>>>>>>>> MyFragment onCreate()
12-26 02:07:35.066 5493-5493/? D/MyFragment3: >>>>>>>>>>>>>>> MyFragment onCreateView()
12-26 02:07:35.068 5493-5493/? I/MyFragment3: onCreate
12-26 02:07:35.068 5493-5493/? D/MyFragment3: >>>>>>>>>>>>>>> MyFragment onActivityCreated()
12-26 02:07:35.069 5493-5493/? I/MyFragment3: onStart
12-26 02:07:35.069 5493-5493/? D/MyFragment3: >>>>>>>>>>>>>>> MyFragment onStart()
12-26 02:07:35.069 5493-5493/? I/MyFragment3: onResume
12-26 02:07:35.069 5493-5493/? D/MyFragment3: >>>>>>>>>>>>>>> MyFragment onResume()
点击返回(关闭) --> Activity--> layout<fragment class --> fragment
12-26 02:14:17.461 5792-5792/liudeli.activity D/MyFragment3: >>>>>>>>>>>>>>> MyFragment onPause()
12-26 02:14:17.461 5792-5792/liudeli.activity I/MyFragment3: Activity onPause
12-26 02:14:18.029 5792-5792/liudeli.activity D/MyFragment3: >>>>>>>>>>>>>>> MyFragment onStop()
12-26 02:14:18.029 5792-5792/liudeli.activity I/MyFragment3: Activity onStop
12-26 02:14:18.029 5792-5792/liudeli.activity D/MyFragment3: >>>>>>>>>>>>>>> MyFragment onDestroyView()
12-26 02:14:18.029 5792-5792/liudeli.activity D/MyFragment3: >>>>>>>>>>>>>>> MyFragment onDestroy()
12-26 02:14:18.029 5792-5792/liudeli.activity D/MyFragment3: >>>>>>>>>>>>>>> MyFragment onDetach()
12-26 02:14:18.029 5792-5792/liudeli.activity I/MyFragment3: Activity onDestroy