(参考android开发文档之Fragments)
Fragments生命周期:
Lifecycle
Though a Fragment's lifecycle is tied to its owning activity, it has its own wrinkle on the standard activity lifecycle. It includes basic activity lifecycle methods such asonResume()
, but also important are methods related to interactions with the activity and UI generation.
The core series of lifecycle methods that are called to bring a fragment up to resumed state (interacting with the user) are:
onAttach(Activity)
called once the fragment is associated with its activity.onCreate(Bundle)
called to do initial creation of the fragment.onCreateView(LayoutInflater, ViewGroup, Bundle)
creates and returns the view hierarchy associated with the fragment.onActivityCreated(Bundle)
tells the fragment that its activity has completed its ownActivity.onCreate()
.onViewStateRestored(Bundle)
tells the fragment that all of the saved state of its view hierarchy has been restored.onStart()
makes the fragment visible to the user (based on its containing activity being started).onResume()
makes the fragment interacting with the user (based on its containing activity being resumed).
As a fragment is no longer being used, it goes through a reverse series of callbacks:
onPause()
fragment is no longer interacting with the user either because its activity is being paused or a fragment operation is modifying it in the activity.onStop()
fragment is no longer visible to the user either because its activity is being stopped or a fragment operation is modifying it in the activity.onDestroyView()
allows the fragment to clean up resources associated with its View.onDestroy()
called to do final cleanup of the fragment's state.onDetach()
called immediately prior to the fragment no longer being associated with its activity.
补充:以上内容比较简单,就不翻译了,
生命周期图如下所示:
生命周期log图:
补充:为了过滤出fragment和activity的生命周期,这里就统一使用TAG=“Fragment1”
图二是手机屏幕暗后该应用的log图:
图三是手机屏幕亮后该应用的log图:
图四是退出该应用后的log图:
log图分析:
从log图可以看出主activity和fragment的生命周期是相对应的,启动时是activity先启动fragment后启动,退出时是fragment先退出activity后退出。
具体的代码实现如下:
java类:
子类Fragment1类继承的父类Fragment:
public class Fragment1 extends Fragment {
private final String TAG = "Fragment1";
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach( activity);
Log. i(TAG , "--Fragment1-->>onAttach" );
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate( savedInstanceState);
Log. i(TAG , "--Fragment1-->>onCreate" );
}
@Override
public View onCreateView(LayoutInflaterinflater, ViewGroup container,
Bundle savedInstanceState) {
Log. i(TAG , "--Fragment1-->>onCreateView" );
View view = inflater.inflate(R.layout. fragment1, null);
return view;
}
@Override
public void onActivityCreated(BundlesavedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated( savedInstanceState);
Log. i(TAG , "--Fragment1-->>onActivityCreated" );
}
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log. i(TAG , "--Fragment1-->>onStart" );
}
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log. i(TAG , "--Fragment1-->>onResume" );
}
@Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log. i(TAG , "--Fragment1-->>onPause" );
}
@Override
public void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log. i(TAG , "--Fragment1-->>onStop" );
}
@Override
public void onDestroyView() {
// TODO Auto-generated method stub
super.onDestroyView();
Log. i(TAG , "--Fragment1-->>onDestroyView" );
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log. i(TAG , "--Fragment1-->>onDestroy" );
}
@Override
public void onDetach() {
// TODO Auto-generated method stub
super.onDetach();
Log. i(TAG , "--Fragment1-->>onDetach" );
}
}
主activity类Main ctivity:
public class MainActivity extends Activity {
//为了过滤出fragment和activity的生命周期,
//这里就统一使用TAG=“Fragment1”
private final String TAG = "Fragment1";
private FragmentManager fragmentManager;
private FragmentTransaction fragmentTransaction;
private Fragment1 fragment1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "--MainActivity-->>onCreate");
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragment1 = new Fragment1();
fragmentTransaction.add(R.id.line1, fragment1);
//fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.i(TAG, "--MainActivity-->>onStart");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.i(TAG, "--MainActivity-->>onResume");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.i(TAG, "--MainActivity-->>onPause");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.i(TAG, "--MainActivity-->>onStop");
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Log.i(TAG, "--MainActivity-->>onRestart");
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.i(TAG, "--MainActivity-->>onDestroy");
}
}
xml布局文件如下:
activity_main.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/line1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#cccccc"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="@+id/line2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ffaaaa"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
fragment1.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" >
<RatingBar
android:id="@+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转到fragment2" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转到fragment3" />
</LinearLayout>
手机显示的效果图如下: