Fragments学习之《Fragments生命周期》

(参考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:

  1. onAttach(Activity) called once the fragment is associated with its activity.
  2. onCreate(Bundle) called to do initial creation of the fragment.
  3. onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment.
  4. onActivityCreated(Bundle) tells the fragment that its activity has completed its ownActivity.onCreate().
  5. onViewStateRestored(Bundle) tells the fragment that all of the saved state of its view hierarchy has been restored.
  6. onStart() makes the fragment visible to the user (based on its containing activity being started).
  7. 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:

  1. 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.
  2. 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.
  3. onDestroyView() allows the fragment to clean up resources associated with its View.
  4. onDestroy() called to do final cleanup of the fragment's state.
  5. onDetach() called immediately prior to the fragment no longer being associated with its activity.

补充:以上内容比较简单,就不翻译了,微笑

生命周期图如下所示:

生命周期log图:

补充:为了过滤出fragment和activity的生命周期,这里就统一使用TAG=“Fragment1”

图一是该应用启动后的log图:








图二是手机屏幕暗后该应用的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>


手机显示的效果图如下:




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值