Android Fragment生命周期及静态加载

Posted 语风6649

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android Fragment生命周期及静态加载相关的知识,希望对你有一定的参考价值。

一个Fragment必须总是被嵌入到一个Activity中,它的生命周期直接被其所属的宿主Activity生命周期影响,它的状态会随宿主的状态变化而变化。

 

要创建一个Fragment 必须创建一个Fragment的子类,或者继承自另一个已经存在的Fragment的子类.并重写onCreateView()方法加载UI。

 

静态加载两个Fragment,左边显示三个Button,右边显示一个TextView

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:id="@+id/activity_main"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="vertical"
 7     tools:context="com.example.lesson10_fragment.MainActivity">
 8 
 9     <fragment
10         android:tag="fragment1"
11         android:name="com.example.lesson10_fragment.Fragment1"
12         android:layout_width="match_parent"
13         android:layout_height="wrap_content" />
14 
15 
16     <fragment
17         android:tag="fragment2"
18         android:name="com.example.lesson10_fragment.Fragment2"
19         android:layout_width="match_parent"
20         android:layout_height="wrap_content" />
21 
22 </LinearLayout>
activity_main.xml
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="match_parent"
 4     android:orientation="vertical">
 5 
 6     <Button
 7         android:layout_width="wrap_content"
 8         android:layout_height="wrap_content"
 9         android:text="Button1"/>
10     <Button
11         android:layout_width="wrap_content"
12         android:layout_height="wrap_content"
13         android:text="Button2"/>
14     <Button
15         android:layout_width="wrap_content"
16         android:layout_height="wrap_content"
17         android:text="Button3"/>
18 </LinearLayout>
frag1_layout.xml
 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="match_parent">
 4 
 5     <TextView
 6         android:layout_width="wrap_content"
 7         android:layout_height="wrap_content"
 8         android:text="哈哈哈啊哈哈"
 9         android:textSize="50sp"
10         android:layout_centerInParent="true"/>
11 
12 </RelativeLayout>
frag2_layout.xml
 1 public class Fragment1 extends Fragment{
 2 
 3     @Override
 4     public void onAttach(Context context) {
 5         super.onAttach(context);
 6         Log.e("TAG","----------Fragment1----onAttach");
 7     }
 8 
 9     @Override
10     public void onCreate(@Nullable Bundle savedInstanceState) {
11         super.onCreate(savedInstanceState);
12         Log.e("TAG","----------Fragment1----onCreate");
13     }
14 
15     @Nullable
16     @Override
17     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
18         View layout = View.inflate(getActivity(),R.layout.frag1_layout,null);
19         //由于这super.onCreateView(inflater, container, savedInstanceState)返回的是null,所以不能用
20 
21         Log.e("TAG","----------Fragment1----onCreateView");
22         return layout;
23     }
24 
25     @Override
26     public void onActivityCreated(@Nullable Bundle savedInstanceState) {
27         super.onActivityCreated(savedInstanceState);
28         Log.e("TAG","----------Fragment1----onActivityCreated");
29     }
30 
31     @Override
32     public void onStart() {
33         super.onStart();
34         Log.e("TAG","----------Fragment1----onStart");
35     }
36 
37     @Override
38     public void onResume() {
39         super.onResume();
40         Log.e("TAG","----------Fragment1----onResume");
41     }
42 
43     @Override
44     public void onPause() {
45         super.onPause();
46         Log.e("TAG","----------Fragment1----onPause");
47     }
48 
49     @Override
50     public void onStop() {
51         super.onStop();
52         Log.e("TAG","----------Fragment1----onStop");
53     }
54 
55     @Override
56     public void onDestroyView() {
57         super.onDestroyView();
58         Log.e("TAG","----------Fragment1----onDestroyView");
59     }
60 
61     @Override
62     public void onDestroy() {
63         super.onDestroy();
64         Log.e("TAG","----------Fragment1----onDestroy");
65     }
66 
67     @Override
68     public void onDetach() {
69         super.onDetach();
70         Log.e("TAG","----------Fragment1----onDetach");
71     }
72 }
Fragment1.java
1 public class Fragment2 extends Fragment{
2 
3     @Nullable
4     @Override
5     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
6         View layout = View.inflate(getActivity(),R.layout.frag2_layout,null);
7         return layout;
8     }
9 }
Fragment2.java
 1 public class MainActivity extends AppCompatActivity {
 2 
 3     @Override
 4     protected void onCreate(Bundle savedInstanceState) {
 5         super.onCreate(savedInstanceState);
 6         setContentView(R.layout.activity_main);
 7         Log.e("TAG","---MainActivity----onCreate");
 8     }
 9 
10     @Override
11     protected void onStart() {
12         super.onStart();
13         Log.e("TAG","---MainActivity----onStart");
14     }
15 
16     @Override
17     protected void onResume() {
18         super.onResume();
19         Log.e("TAG","---MainActivity----onResume");
20     }
21 
22     @Override
23     protected void onPause() {
24         super.onPause();
25         Log.e("TAG","---MainActivity----onPause");
26     }
27 
28     @Override
29     protected void onStop() {
30         super.onStop();
31         Log.e("TAG","---MainActivity----onStop");
32     }
33 
34     @Override
35     protected void onDestroy() {
36         super.onDestroy();
37         Log.e("TAG","---MainActivity----onDestroy");
38     }
39 
40     @Override
41     protected void onRestart() {
42         super.onRestart();
43         Log.e("TAG","---MainActivity----onRestart");
44     }
45 }
MainActivity.java

 

运行之后

 

以上是关于Android Fragment生命周期及静态加载的主要内容,如果未能解决你的问题,请参考以下文章

重温Android和Fragment生命周期

Android:Fragment

Android Fragment + ViewPager的懒加载实现

Android Fragment + ViewPager的懒加载实现

避免切换横竖屏Fragment的重复加载导致UI混乱

Android基础——控件的混合生命周期