android3.0引入了fragment这个概念。它在一个活动中可以是用户界面或行为。下面我们就来一探究竟:
fragment生命周期
fragment和activity是紧密结合在一起的,虽然有自己的生命周期,但是activity的生命周期直接影响fragment的生命周期:
该图来源于:(file:///D:/AndroidSDK/docs/images/fragment_lifecycle.png)
fragment生命周期中,我们关注的至少有三个生命周期方法:onCreate、onCreateView、onPause。而在onCreateView方法中就可以创建用户界面了。
添加fragment到activity中
添加fragment到activity中有两种方法:
一、在layout文件中中添加fragment,例如下面代码将TestFragments作为UI添加到某个activity中
<fragment android:name="grp.rp.android.TestFragments" android:id="@+id/fragmentId"
android:layout_width="fill_parent" android:layout_height="wrap_content"/>
二、使用FragmentManager
此时需要先获得一个FragmentTransaction对象,然后调用FragmentTransaction的add方法将一个fragment作为UI添加到activity。
如:
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
TestFragments test = new TestFragments();
ft.add(R.id.ll, test);
ft.commit();
注意:要使fragment生效需要调用commit方法。
下面就写个简单的例子,首先还是将工程结构图给出:
FragmentsTestActivity.java
public class FragmentsTestActivity extends Activity {
TextView mTv ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//one method to use fragment .
// setContentView(R.layout.main);
// FragmentManager fm = getFragmentManager();
// TestFragments tf = (TestFragments)fm.findFragmentById(R.id.fragmentId);
// mTv = (TextView) findViewById(R.id.fEditText);
// mTv.setText(tf.getLifeCycleString());
//the other method to use fragment.
setContentView(R.layout.second_main);
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
TestFragments test = new TestFragments();
ft.add(R.id.ll, test);
ft.commit();
}
}
TestFragments.java
public class TestFragments extends Fragment {
private StringBuilder mSb = new StringBuilder(100);
public TestFragments(){}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mSb.append("fragments Life cycle --> onActivityCreated called !\n");
Log.i("tag", "fragments Life cycle --> onActivityCreated called !\n");
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mSb.append("fragments Life cycle --> onAttach called !\n");
Log.i("tag", "fragments Life cycle --> onAttach called !\n");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSb.append("fragments Life cycle --> onCreate called !\n");
Log.i("tag", "fragments Life cycle --> onCreate called !\n");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mSb.append("fragments Life cycle --> onCreateView called !\n");
Log.i("tag", "fragments Life cycle --> onCreateView called !\n");
return inflater.inflate(R.layout.fragments_layout, container, false);
}
@Override
public void onDestroy() {
super.onDestroy();
mSb.append("fragments Life cycle --> onDestroy called !\n");
Log.i("tag", "fragments Life cycle --> onDestroy called !\n");
}
@Override
public void onDetach() {
super.onDetach();
mSb.append("fragments Life cycle --> onDetach called !\n");
Log.i("tag", "fragments Life cycle --> onDetach called !\n");
}
@Override
public void onPause() {
super.onPause();
mSb.append("fragments Life cycle --> onPause called !\n");
Log.i("tag", "fragments Life cycle --> onPause called !\n");
}
@Override
public void onResume() {
super.onResume();
mSb.append("fragments Life cycle --> onResume called !\n");
Log.i("tag", "fragments Life cycle --> onResume called !\n");
}
@Override
public void onStart() {
super.onStart();
mSb.append("fragments Life cycle --> onStart called !\n");
Log.i("tag", "fragments Life cycle --> onStart called !\n");
}
@Override
public void onStop() {
super.onStop();
mSb.append("fragments Life cycle --> onStop called !\n");
Log.i("tag", "fragments Life cycle --> onStop called !\n");
}
public String getLifeCycleString(){
return mSb.toString();
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<fragment android:name="grp.rp.android.TestFragments"
android:id="@+id/fragmentId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
second_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
fragments_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/fEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.49" >
</EditText>
</LinearLayout>
就这些了,简单吧!