一个Fragment必须总是被嵌入到一个Activity中,它的生命周期直接被其所属的宿主Activity生命周期影响,它的状态会随宿主的状态变化而变化。
要创建一个Fragment 必须创建一个Fragment的子类,或者继承自另一个已经存在的Fragment的子类.并重写onCreateView()方法加载UI。
静态加载两个Fragment,左边显示三个Button,右边显示一个TextView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.lesson10_fragment.MainActivity"> <fragment
android:tag="fragment1"
android:name="com.example.lesson10_fragment.Fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> <fragment
android:tag="fragment2"
android:name="com.example.lesson10_fragment.Fragment2"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> </LinearLayout>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3"/>
</LinearLayout>
frag1_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="哈哈哈啊哈哈"
android:textSize="50sp"
android:layout_centerInParent="true"/> </RelativeLayout>
frag2_layout.xml
public class Fragment1 extends Fragment{ @Override
public void onAttach(Context context) {
super.onAttach(context);
Log.e("TAG","----------Fragment1----onAttach");
} @Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("TAG","----------Fragment1----onCreate");
} @Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View layout = View.inflate(getActivity(),R.layout.frag1_layout,null);
//由于这super.onCreateView(inflater, container, savedInstanceState)返回的是null,所以不能用 Log.e("TAG","----------Fragment1----onCreateView");
return layout;
} @Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.e("TAG","----------Fragment1----onActivityCreated");
} @Override
public void onStart() {
super.onStart();
Log.e("TAG","----------Fragment1----onStart");
} @Override
public void onResume() {
super.onResume();
Log.e("TAG","----------Fragment1----onResume");
} @Override
public void onPause() {
super.onPause();
Log.e("TAG","----------Fragment1----onPause");
} @Override
public void onStop() {
super.onStop();
Log.e("TAG","----------Fragment1----onStop");
} @Override
public void onDestroyView() {
super.onDestroyView();
Log.e("TAG","----------Fragment1----onDestroyView");
} @Override
public void onDestroy() {
super.onDestroy();
Log.e("TAG","----------Fragment1----onDestroy");
} @Override
public void onDetach() {
super.onDetach();
Log.e("TAG","----------Fragment1----onDetach");
}
}
Fragment1.java
public class Fragment2 extends Fragment{ @Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View layout = View.inflate(getActivity(),R.layout.frag2_layout,null);
return layout;
}
}
Fragment2.java
public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e("TAG","---MainActivity----onCreate");
} @Override
protected void onStart() {
super.onStart();
Log.e("TAG","---MainActivity----onStart");
} @Override
protected void onResume() {
super.onResume();
Log.e("TAG","---MainActivity----onResume");
} @Override
protected void onPause() {
super.onPause();
Log.e("TAG","---MainActivity----onPause");
} @Override
protected void onStop() {
super.onStop();
Log.e("TAG","---MainActivity----onStop");
} @Override
protected void onDestroy() {
super.onDestroy();
Log.e("TAG","---MainActivity----onDestroy");
} @Override
protected void onRestart() {
super.onRestart();
Log.e("TAG","---MainActivity----onRestart");
}
}
MainActivity.java
运行之后