Android Fragment生命周期及静态加载

少年恃险若平地,独倚长剑凌清秋。这篇文章主要讲述Android Fragment生命周期及静态加载相关的知识,希望能为你提供帮助。
一个Fragment必须总是被嵌入到一个Activity中,它的生命周期直接被其所属的宿主Activity生命周期影响,它的状态会随宿主的状态变化而变化。
 
要创建一个Fragment 必须创建一个Fragment的子类,或者继承自另一个已经存在的Fragment的子类.并重写onCreateView()方法加载UI。
 

Android Fragment生命周期及静态加载

文章图片

静态加载两个Fragment,左边显示三个Button,右边显示一个TextView
Android Fragment生命周期及静态加载

文章图片
Android Fragment生命周期及静态加载

文章图片
1 < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2xmlns:tools="http://schemas.android.com/tools" 3android:id="@+id/activity_main" 4android:layout_width="match_parent" 5android:layout_height="match_parent" 6android:orientation="vertical" 7tools:context="com.example.lesson10_fragment.MainActivity"> 8 9< fragment 10android:tag="fragment1" 11android:name="com.example.lesson10_fragment.Fragment1" 12android:layout_width="match_parent" 13android:layout_height="wrap_content" /> 14 15 16< fragment 17android:tag="fragment2" 18android:name="com.example.lesson10_fragment.Fragment2" 19android:layout_width="match_parent" 20android:layout_height="wrap_content" /> 21 22 < /LinearLayout>

activity_main.xml
Android Fragment生命周期及静态加载

文章图片
Android Fragment生命周期及静态加载

文章图片
1 < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2android:layout_width="match_parent" 3android:layout_height="match_parent" 4android:orientation="vertical"> 5 6< Button 7android:layout_width="wrap_content" 8android:layout_height="wrap_content" 9android:text="Button1"/> 10< Button 11android:layout_width="wrap_content" 12android:layout_height="wrap_content" 13android:text="Button2"/> 14< Button 15android:layout_width="wrap_content" 16android:layout_height="wrap_content" 17android:text="Button3"/> 18 < /LinearLayout>

frag1_layout.xml
Android Fragment生命周期及静态加载

文章图片
Android Fragment生命周期及静态加载

文章图片
1 < RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2android:layout_width="match_parent" 3android:layout_height="match_parent"> 4 5< TextView 6android:layout_width="wrap_content" 7android:layout_height="wrap_content" 8android:text="哈哈哈啊哈哈" 9android:textSize="50sp" 10android:layout_centerInParent="true"/> 11 12 < /RelativeLayout>

frag2_layout.xml
Android Fragment生命周期及静态加载

文章图片
Android Fragment生命周期及静态加载

文章图片
1 public class Fragment1 extends Fragment{ 2 3@Override 4public void onAttach(Context context) { 5super.onAttach(context); 6Log.e("TAG","----------Fragment1----onAttach"); 7} 8 9@Override 10public void onCreate(@Nullable Bundle savedInstanceState) { 11super.onCreate(savedInstanceState); 12Log.e("TAG","----------Fragment1----onCreate"); 13} 14 15@Nullable 16@Override 17public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 18View layout = View.inflate(getActivity(),R.layout.frag1_layout,null); 19//由于这super.onCreateView(inflater, container, savedInstanceState)返回的是null,所以不能用 20 21Log.e("TAG","----------Fragment1----onCreateView"); 22return layout; 23} 24 25@Override 26public void onActivityCreated(@Nullable Bundle savedInstanceState) { 27super.onActivityCreated(savedInstanceState); 28Log.e("TAG","----------Fragment1----onActivityCreated"); 29} 30 31@Override 32public void onStart() { 33super.onStart(); 34Log.e("TAG","----------Fragment1----onStart"); 35} 36 37@Override 38public void onResume() { 39super.onResume(); 40Log.e("TAG","----------Fragment1----onResume"); 41} 42 43@Override 44public void onPause() { 45super.onPause(); 46Log.e("TAG","----------Fragment1----onPause"); 47} 48 49@Override 50public void onStop() { 51super.onStop(); 52Log.e("TAG","----------Fragment1----onStop"); 53} 54 55@Override 56public void onDestroyView() { 57super.onDestroyView(); 58Log.e("TAG","----------Fragment1----onDestroyView"); 59} 60 61@Override 62public void onDestroy() { 63super.onDestroy(); 64Log.e("TAG","----------Fragment1----onDestroy"); 65} 66 67@Override 68public void onDetach() { 69super.onDetach(); 70Log.e("TAG","----------Fragment1----onDetach"); 71} 72 }

Fragment1.java
Android Fragment生命周期及静态加载

文章图片
Android Fragment生命周期及静态加载

文章图片
1 public class Fragment2 extends Fragment{ 2 3@Nullable 4@Override 5public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 6View layout = View.inflate(getActivity(),R.layout.frag2_layout,null); 7return layout; 8} 9 }

Fragment2.java
Android Fragment生命周期及静态加载

文章图片
Android Fragment生命周期及静态加载

文章图片
1 public class MainActivity extends AppCompatActivity { 2 3@Override 4protected void onCreate(Bundle savedInstanceState) { 5super.onCreate(savedInstanceState); 6setContentView(R.layout.activity_main); 7Log.e("TAG","---MainActivity----onCreate"); 8} 9 10@Override 11protected void onStart() { 12super.onStart(); 13Log.e("TAG","---MainActivity----onStart"); 14} 15 16@Override 17protected void onResume() { 18super.onResume(); 19Log.e("TAG","---MainActivity----onResume"); 20} 21 22@Override 23protected void onPause() { 24super.onPause(); 25Log.e("TAG","---MainActivity----onPause"); 26} 27 28@Override 29protected void onStop() { 30super.onStop(); 31Log.e("TAG","---MainActivity----onStop"); 32} 33 34@Override 35protected void onDestroy() { 36super.onDestroy(); 37Log.e("TAG","---MainActivity----onDestroy"); 38} 39 40@Override 41protected void onRestart() { 42super.onRestart(); 43Log.e("TAG","---MainActivity----onRestart"); 44} 45 }

MainActivity.java 
运行之后
Android Fragment生命周期及静态加载

文章图片

Android Fragment生命周期及静态加载

文章图片

【Android Fragment生命周期及静态加载】 

    推荐阅读